iphone - NSInternalInconsistencyException - 无法加载 bundle : 中的 NIB

标签 iphone ios uiviewcontroller bundle resourcebundle

我正在尝试将所有 .xib 资源 bundle 到“ViewController.bundle”中,以便与“MyLibrary.framework”一起发布。如果我只是将所有 .xib 文件放在框架项目中,一切都会正常工作,但如果我将它们放在“ViewController.bundle”中,一切都会崩溃

*** 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)

我尝试过多种方法: 1. 在Finder中创建“ViewController.bundle”,将所有.xib放在那里,然后拖到xcode 2.为框架项目创建一个新的Target,在“OS X”部分的“Framework & Library”下选择“Bundle”(注意:它必须是OSX,因为我使用的是xcode 4.6,其下没有“Bundle” “框架和库” - 它曾经有,我曾经使用此向导创建包含 .png 资源的 bundle )

在这两种情况下,我都确保“ViewController.bundle”存在于我的目标应用程序中的“构建阶段”>>“复制 bundle 资源”中。 我还尝试在我的框架项目的“目标依赖项”下包含“ViewController.bundle”,但我的所有努力都导致了相同的崩溃。

我也尝试了各种方法来加载.xib,但没有成功。但是,以下从同一“ViewController.bundle”加载 .png 资源的代码可以完美运行:

// 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;

使用相同的代码加载xib失败:

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!" );
}];

我花了过去 8 个小时对此进行调试,谷歌搜索并尝试了 SO 的各种建议,但无济于事......非常感谢任何帮助!谢谢!

最佳答案

我尝试的第二种方法是最接近的...然后在build设置中将 OSX bundle 修改为 iOS bundle 。显然,我错过了几个步骤。我在这里遵循了 Matt Galloway 的教程和示例项目:

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

现在一切都按预期进行。

感谢大家的帮助!

关于iphone - NSInternalInconsistencyException - 无法加载 bundle : 中的 NIB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15520595/

相关文章:

ios - 您是否必须使用Xcode 4.3.1(或更高版本)进行编译才能在新iPad上使用@ 2x图像?

ios - 更改推送 View Controller 的大小

ios - 条形图未缩放至 BarChartView 的大小

swift - 从嵌入式 UIPageViewController 访问父 UIViewController 的成员

ios - 如何向 NSDate 添加周数?

ios - 无法从 Sprite Kit 打开 Game Center

ios - 异步请求未正确更新变量

iphone - 想要在通知事件发生时播放我的声音文件,然后停止

ios - 查看自动添加的约束

iphone - 应用内的评分和评论会将用户带到应用商店,从而离开我们的应用 - 完全不受欢迎