PDA

View Full Version : Setting the view controllerís nib file name in Interface Builder




RossOliver
Jun 15, 2008, 01:51 PM
Hey,

Reading through the apple documentation, I came across this paragraph:


If you use Interface Builder, it is recommended that you set up the views for your view controllers in separate nib filesóthat is, you create a nib file for each application view. In this case, you set the Fileís Owner to your view controller subclass and connect the Fileís Owner view outlet to the root view in the nib file. You can then create the view controller programmatically using the initWithNibName:bundle: method or by setting the view controllerís nib file name in Interface Builder. When you set the nib file name, an instance of your view controller class is created and initialized with the contents of the specified nib file.


How exactly do I set the view controller's nib file name in IB? Also, how would I then go on to create an instance of my view controller so it loads the nib and so I can then add it as a sub view etc...

Full document here: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/UsingViewControllers/chapter_5_section_1.html#//apple_ref/doc/uid/TP40007457-CH5-SW1

Cheers



Eraserhead
Jun 15, 2008, 02:10 PM
Select the file owner and set its class to your custom class. Post again if this doesn't make sense.

RossOliver
Jun 15, 2008, 02:33 PM
Select the file owner and set its class to your custom class. Post again if this doesn't make sense.

Ah, I had done that - I thought there was more to it :confused:...

Another question you (or anyone else) may be able to help with. The MainWindow.xib that is provided - should that be left blank and should I create my main view in another nib, say MainView.xib or should I just create the view in MainWindow.xib?

I ask because it doesn't make sense to me how the MainWindow.xib view would be correctly handled - surely it needs a view controller, but I wouldn't be able to make the view controller it's file's owner. Would this mean I would have to load the view using initWithNib? (that method may not be accurate)

Thanks again.

Eraserhead
Jun 15, 2008, 02:50 PM
You should just stick the main view in the first .xib

With regards to having lots of multiple files there used to be two reasons for having multiple .nib files for everything.

1) Its easier to localise if you are using version control software (http://en.wikipedia.org/wiki/Revision_control). But with the new .xib format that is no longer a big issue. I suppose its a slight issue still as the translators don't know what has changed as clearly, but it shouldn't matter too much.

2) There might be slightly worse performance, to put this in perspective on the Mac I have nibs with 20 windows and that part of loading takes around 1/1000th of a second. On the iPhone this may be more of an issue, but you should be OK with having a small number of views in the same nib without issue, you'll have to performance check this yourself ;).

RossOliver
Jun 16, 2008, 01:18 PM
You should just stick the main view in the first .xib

With regards to having lots of multiple files there used to be two reasons for having multiple .nib files for everything.

1) Its easier to localise if you are using version control software (http://en.wikipedia.org/wiki/Revision_control). But with the new .xib format that is no longer a big issue. I suppose its a slight issue still as the translators don't know what has changed as clearly, but it shouldn't matter too much.

2) There might be slightly worse performance, to put this in perspective on the Mac I have nibs with 20 windows and that part of loading takes around 1/1000th of a second. On the iPhone this may be more of an issue, but you should be OK with having a small number of views in the same nib without issue, you'll have to performance check this yourself ;).

OK, I think I understand now :)

Last question (hopefully) - I have an interface consisting of a UIView, UITableView and a UIButton.

The file's owner is set to a UIViewController which has an IBOutlet for the UIButton and UIView. It also has an IBAction method to handle the UIButton press.

I have a UITableViewController for the UITableView datasource/delegate methods.

What I want to do is refresh the UITableView when the UIButton is pressed, but what I can't work out is how to let the UIViewController know about the UITableViewController so it can refresh the UITableView. Do I just add the UITableView as an IBOutlet in the UIViewController and hook it up in IB?

Cheers