NSViewController self initialisation?!


#1

In the course of investigating how to tidy up many popovers into separate xib files I found this chapter most helpful. However when working through the Apple ‘popover’ sample I just can’t understand how the view controller in that code gets properly initialised with the correct xib without any apparent initWithNibName stuff. The popover view xib is called PopoverView.xib and if you check the value of the view controller’s nib name right after the nib is loaded you’ll find it’s right there as “PopoverView.xib”.
If you change the xib file name, clean the project and re-build you’ll get an exception as the code is still looking for this darned PopoverView.xib.
Please could someone let me know the simple answer here to this apparent conundrum. Thanks.


#2

By default, a view controller’s name is used to locate a nib file if you do not explicitly specify a nib file name when you initialise the controller.
If you change the name of a nib file that a controller is managing, you should then init the controller with the initWithNibName:bundle: method and specify the name of the nib file. (Depending on the version of the Xcode you are using, you can get away without calling initWithNibName:bundle: after you have renamed the file.)


#3

Thanks for the quick reply ibex10. In fact what seems to happen is that the run-time is very easy-going! If there happens to be a nib-file with the prefix of the viewController ivar then the run-time will use it. However if there isn’t such a file and if there is a ‘spare’ nib just lying about it’ll use that. I’m using Xcode 4.6.1 on OS X 10.8.3.
I’d be interested in knowing about the documentation providing some background on this behaviour.


#4

you only need the initWithNubName if the name of the .xib file is different from the name of the viewControllers Object Name.

i.e.
MyFunkyViewController.c excerpt:


@implementation anotherName...

When you initialise it, it will look for ‘anotherName.xib’ The name of the .h/c files is irrelevant

So what they probably had is:

popOverViewController.c excerpt:

@implementation popoverView
... 

The name of the .c and .h files is irrelevant, it’s all about the Object Names in this case.