Search code examples
iphoneiosuiviewcontrollerbundleresourcebundle

NSInternalInconsistencyException - Could not load NIB in bundle:


I am trying to bundle all my .xib resources into a "ViewController.bundle" to ship along with "MyLibrary.framework". If I just place all the .xib files in the framework project, everything is working fine, but if I put them inside "ViewController.bundle", everything break apart

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Could not load NIB in bundle: 
'NSBundle </Users/administrator/Library/Application Support/iPhone Simulator/5.0/
Applications/8AD68FD1-158E-4926-AE03-8FEF870E7012/ios.app/ViewController.bundle> 
(not yet loaded)' with name 'FirstViewController_iPhone''
*** First throw call stack:
(0x160e052 0x1071d0a 0x15b6a78 0x15b69e9 0x3ff838 0x2a6e2c 0x2a73a9 0x2a75cb 0x2c2b89 0x2c29bd  
0x2c0f8a 0x2c0d7e 0x2c0a22 0x2bfd88 0x5e3ac 0x2df8 0x2e34 0x160fec9 0x1e45c2 0x1e455a 0x289b76 
0x28a03f 0x2892fe 0x209a30 0x209c56 0x1f0384 0x1e3aa9 0x1eedfa9 0x15e21c5 0x1547022 0x154590a    
0x1544db4 0x1544ccb 0x1eec879 0x1eec93e 0x1e1a9b 0x2452 0x2385)

I have tried various ways: 1. Creating the "ViewController.bundle" in Finder, place all the .xib there, and drag to xcode 2. Create a new Target for the framework project, choose "Bundle" under "Framework & Library" in "OS X" section (Note: it has to be OSX since I am using xcode 4.6 which do not have "Bundle" under its "Framework & Library" - it used to have, and I used to create bundle that contains .png resources using this wizard)

Under both circumstances, I have made sure that the "ViewController.bundle" exist in "Build Phase" >> "Copy Bundle Resources" in my target app. I also tried including "ViewController.bundle" under "Target Dependencies" of my Framework project, but all my efforts resulted in the same crash.

I also tried various ways to load the .xib, to no avail. However, the following code that load a .png resource from the same "ViewController.bundle" works perfectly:

// Loading .png resource works perfectly
//  UIImage* closeImage = [UIImage imageNamed:@"ViewController.bundle/first.png"];
//  NSString *szBundlePath = [[NSBundle mainBundle] pathForResource:@"ViewController" ofType:@"Bundle"];
//  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"showView" message:szBundlePath delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
//  UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)];
//  [imageView setImage:closeImage];
//  [alertView addSubview:imageView];
//  [alertView show];
//  return;

Using the same code to load the xib failed:

NSBundle *bViewController = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"ViewController" withExtension:@"bundle"]];
if ( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone ) {
    self.m_pFirstViewController = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:bViewController] autorelease];
    self.m_pSecondViewController = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:bViewController] autorelease];
} else {
    self.m_pFirstViewController = [[[FirstViewController alloc] initWithNibName:@"ViewController.bundle/FirstViewController_iPad" bundle:nil] autorelease];
    self.m_pSecondViewController = [[[SecondViewController alloc] initWithNibName:@"ViewController.bundle/SecondViewController_iPad" bundle:nil] autorelease];
}

    /** It crash in this line!! Note however, both m_pFirstViewController and m_pSecondViewController is allocated and not nil */
self.viewControllers = @[self.m_pFirstViewController, self.m_pSecondViewController];

    /** never reach here already crash */
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:self animated:YES completion:^{
    NSLog( @"presented!" );
}];

I have spent the last 8 hours debugging this, googling and tried various suggestions from SO, but to no avail... any help is greatly appreciated! Thanks!


Solution

  • The 2nd way that I tried is the closest... Then modify the OSX bundle to iOS bundle in the Build Settings. Apparently, there are several steps that I missed. I followed the tutorial and sample project by Matt Galloway here:

    http://www.galloway.me.uk/tutorials/ios-library-with-resources/

    and now everything works as intended.

    Thanks everyone for your help!