Search code examples
iosxcodeuiviewcontrollernsbundle

Loading a nib file


I am trying to load a nib file like this:

[[NSBundle mainBundle] loadNibNamed:@"PhotoViewController" owner:self options:nil];

but it makes the app crash


Solution

  • If you're creating the view controller programmatically, you can use the initWithNibName:bundle: view controller method

    I.e.

    [[myViewcontroller alloc] initWithNibName:@"myNib" bundle:nil];
    

    If you're transitioning to the view controller via storyboard, and want to have the storyboard load that view controller from a particular nib file, you could do the following:

    1. In the storyboard, delete the view controller's view
    2. Give the nib file the same name as the class of the storyboard view controller

    This same question is also answered here: Using XCode storyboard to instantiate view controller that uses XIB for its design