Search code examples
objective-cxcodepushviewcontroller

Could not load NIB in bundle with name 'SecondViewController'


I'm trying to push the Navigator Controller to a different view controller but I keep getting the error saying the nib bundle name can't be loaded. Here's the code I'm using:

SecondViewController *vc1 = [[SecondViewController alloc]
      initWithNibName:NSStringFromClass([SecondViewController class]) bundle:Nil];
[self.navigationController pushViewController:vc1 animated:YES];

Solution

  • You have to pass the name of the nib file, not the class name:

    // Assuming there is a properly set up SecondViewController.xib in your project.
    SecondViewController *vc1 = [[SecondViewController alloc] 
                                initWithNibName:@"SecondViewController" bundle:nil];
    [self.navigationController pushViewController:vc1 animated:YES];
    

    EDIT:

    If you're using Storyboards, you have to load your GUI a bit differently:

    UIStoryboard *sboard = [UIStoryboard storyboardWithName:@"StoryboardFileName" 
                                                     bundle:nil];
    SecondViewController *vc1 = [sboard instantiateInitialViewController];