Tim
Tim

Reputation: 14446

What does nibNameOrNil really mean?

The default init method signature on XCode-generated view controllers is:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ }

I've seen these initialized with both values supplied, just the nib name (with bundle as nil), or just nil as both. All seem to work.

How does the UIViewController really handle self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];? Is there a disadvantage to just passing in nil for both values?

Upvotes: 6

Views: 3242

Answers (2)

Richard J. Ross III
Richard J. Ross III

Reputation: 55583

From the docs:

nibName:

If you specify nil for the nibName parameter and you do not override the loadView method, the view controller searches for a nib file using other means. See nibName.

nibBundle:

The bundle in which to search for the nib file. This method looks for the nib file in the bundle's language-specific project directories first, followed by the Resources directory. If nil, this method looks for the nib file in the main bundle.

Upvotes: 1

Rog
Rog

Reputation: 18670

If you pass nil as the nibName, the method will look for a nib with the same filename as your view controller.

For instance, if you have a view controller called MyViewController it will look for a MyViewController.xib nib file.

If no nib is found, you will need to override the loadView method to create and assign a UIView to the controller's view outlet.

- (void)loadView
{
    UIView *theView = [[UIView alloc] ...
    // Setup the main view
    self.view = theView;
}

Upvotes: 9

Related Questions