You can create whatever view structure you want in Interface Builder and then instantiate it using the UINib class. Once you create an UINib object it loads the contents from the nib and keeps them. Then, whenever you send it the instantiateWithOwner:options: message, it will instantiate the objects contained in the xib and return an array with the top level views.
You can then add these views to your view hierarchy and handle them just like any other view you created programmatically If you keep the UINib object (as a property for example), you can instantiate the contents again and again, which allows your xib to be used like a template update: For a pre-iOS 4 workaround see my recent question and answer.
You can create whatever view structure you want in Interface Builder and then instantiate it using the UINib class. Once you create an UINib object it loads the contents from the nib and keeps them. Then, whenever you send it the instantiateWithOwner:options: message, it will instantiate the objects contained in the xib and return an array with the top level views.
You can then add these views to your view hierarchy and handle them just like any other view you created programmatically. If you keep the UINib object (as a property for example), you can instantiate the contents again and again, which allows your xib to be used like a template. Update: For a pre-iOS 4 workaround see my recent question and answer.
Thank you Toastor, this sound like what I need. I noticed in the documentation that it is an iOS 4.0 addition, will it not work on a device running 3. X?
Also the documentation does not mention how you would build the NIB (dealing with outlets etc. ) if a NIB view is to be wrapped. Could you point me at a resource where I could see how this functionality is implemented. Thanks again.
– RickiG Sep 21 '10 at 10:32 I'm sorry I don't know any ressource, I figured things out myself. Within your nib, you could specify the file's owner as usual and make the appropriate connections. They will be the same for all instances, though, so you need to set tags on your new views right after instantiating to be able to distinguish between them later.
As for the pre-iOS 4 replacement - I had the exact same question. I'll update my answer with a link to the question and answer right away. – Toastor Sep 21 '10 at 10:48 I went down the loadNibNamed path, as you state in ther question,not to abandon 3.
X users. I am starting to feel a bit stupid, I can find no reference to how I should build my XIB, should my XIB view have a "Class" in IB, what is file owner if I load it at runtime? Where to do the IBOltlet/IBAction belong when doing it like this.
Apple does it like this in their taggedLocation:NSBundle mainBundle loadNibNamed:@"EventTableViewCell" owner:self options:nil; they don't assign it to an array, they just type out the line above and it works? Thank you again for trying, I am completely lost now:) – RickiG Sep 21 '10 at 11:50 You do it like you would for any nib. Say you have a viewcontroller and add the contents of the nib to the VC's view.
The VC has a method -(IBAction)buttonPressed: (id)sender;. If in your nib you add a button and set the files owner to your VC subclass, you can connect the action to the button as usual. The action method will be called when tapping ANY instance of that button however, that's why you need tags if you instantiate the nib more then just once.
Otherwise you wouldn't know which button has been tapped. – Toastor Sep 21 '10 at 12:09 As for the array-thing: When instantiating a nib, there may be more then one top-level view, which is why the return value is an array. You don't need to assign it, but you need to access its content to get a reference to the view that the nib contains... – Toastor Sep 21 '10 at 12:10.
Have my UIViewController implement what ever methods the customView protocol required. Should I first build a MyCustomView class extending the UIView class, have it hold all my IBOutlets, set MyCustomClass as files owner and then instantiate that as shown above? Is it OK to have a view act as viewController for the IB view and how would I relay actions to my "real" viewController?
What I would like to achieve is to deal with instantiating and laying out several UIViews in my UIViewControllers code, but have the freedom of designing some of these UIViews in IB. All the info I can find is regarding the standard "build a UIViewController with a XIB for the view" or "How to build libraries of IB components".
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.