Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 269
Tim Avatar asked Feb 20 '12 21:02

Tim


1 Answers

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;
}
like image 117
Rog Avatar answered Sep 16 '22 15:09

Rog