views:

185

answers:

3

I am a little curious, I have a view controller class and an NIB/XIB (both are named "MapViewController") If I do the following it loads the NIB with the matching name.

-(id)init {
    self = [super initWithNibName:@"MapViewController" bundle:nil];
    if(self) {
        do things ...
    }
    return self;
}

if on the other hand I just specify [super init] does Xcode just look for a NIB that matches the name of the controller, is that how this is working?

-(id)init {
    self = [super init];
    if(self) {
        do things ...
    }
    return self;
}

cheers Gary.

+4  A: 

From the documentation:

If you specify nil for the nibName parameter and do not override the loadView method in your custom subclass, the default view controller behavior is to look for a nib file whose name (without the .nib extension) matches the name of your view controller class. If it finds one, the class name becomes the value of the nibName property, which results in the corresponding nib file being associated with this view controller.

Graham Lee
Thank you, I see it now, I was looking under init and getting lost.
fuzzygoat
A: 

does Xcode just look for a NIB that matches the name of the controller

Pretty much:

If you specify nil for the nibName parameter and do not override the loadView method in your custom subclass, the default view controller behavior is to look for a nib file whose name (without the .nib extension) matches the name of your view controller class. If it finds one, the class name becomes the value of the nibName property, which results in the corresponding nib file being associated with this view controller.

Typeoneerror
+2  A: 

Yes, in this particular case it will work. According to the UIViewController, calling init is similar to calling initWithNibName:bundle: with nil as nib name:

If you specify nil for the nibName parameter and do not override the loadView method in your custom subclass, the default view controller behavior is to look for a nib file whose name (without the .nib extension) matches the name of your view controller class. If it finds one, the class name becomes the value of the nibName property, which results in the corresponding nib file being associated with this view controller.

Laurent Etiemble