'NSInternalInconsistencyException',原因:无法在包中加载NIB:'NSBundle

60

在我的AppDelegate中有一个我不理解的问题。RootViewController最初被称为ViewController,我改了它的名字。应用程序由许多ViewController组成,然后我引入了UINavigationController。为什么会出现这个错误?

  NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle 
  /Users/XXXXXXXXXXXX/Library/Application Support/iPhone simulator/6.0/Applications/
  B7A7D461-1CFE-4B05-AF32-00B65FCFFF49/XXXXXXXXXX.app> (loaded)'with name 
 'RootViewController''
  
  *** First throw call stack:
  (0x1992012 0x1357e7e 0x1991deb 0x4bafac 0x37fe37 0x380418 0x380648 0x380882 0
  x380b2a  0x397ef5 0x397fdb x398286 0x398381 0x398eab 0x398fc9 0x399055 0x49e3ab 
  0x2ef92d 0x136b6b0 0x1f12fc0 0x1f0733c 0x1f12eaf 0x38e8cd 0x2d71a6 0x2d5cbf 
  0x2d5bd9 0x2d4e34 0x2d4c6e 0x2d5a29 0x2d8922 0x382fec 0x2cfbc4 0x2cfdbf 
  0x2cff55 0x2d8f67 0x2b30 0x29c7b7 0x29cda7 0x29dfab 0x2af315 0x2b024b 0x2a1cf8 
  0x1dbedf9 0x1dbead0 0x1907bf5 0x1907962 0x1938bb6 0x1937f44 0x1937e1b 0x29d7da
  0x29f65c 0x269d 0x25c5) 
  ibc++abi.dylib: terminate called throwing an exception
  (lldb) 

这是在AppDelegate.h中的代码

  #import <UIKit/UIKit.h>

  @class RootViewController;

  @interface AppDelegate : UIResponder <UIApplicationDelegate>{

       UINavigationController *navigationController;
   }

  @property (strong, nonatomic) UIWindow *window;

  @property (strong, nonatomic) RootViewController *viewController;
  @property (strong, nonatomic) UINavigationController * navigationController;



  @end

这是AppDelegate.m的代码

  #import "AppDelegate.h"

  #import "RootViewController.h"



  @implementation AppDelegate

  @synthesize navigationController;
  @synthesize viewController;
  @synthesize window;



  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
 {
         self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.

         if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
               self.viewController = [[RootViewController alloc]      initWithNibName:@"RootViewController_iPhone.xib" bundle:nil];
   } else {
    self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPad.xib" bundle:nil];
   }

    RootViewController *rootMenu=[[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];

    self.navigationController =[[UINavigationController alloc]initWithRootViewController:rootMenu];

    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

7
尝试从“Product-> Clean”清理您的项目,对我有用。 - uniruddh
重新运行项目对我有用。 - iYoung
我使用了强制语言功能,所以我改变了bundle。在更改语言后,我可以加载本地化的字符串和HTML文件。但是对于自定义的xib文件,我遇到了这个问题(“无法在bundle中加载NIB”)。请问我错过了什么? - Suresh Durishetti
对于故事板,这是我使用的方法 https://dev59.com/T2435IYBdhLWcg3wkBBe#56199366 - swiftBoy
清理和构建对我有用,谢谢! - Ethan Zhao
21个回答

135

当您在XCode之外重命名文件时,可能会发生此错误。 解决方法是从项目中删除文件(右键单击-删除并“删除引用”)。

然后,您可以重新导入文件到您的项目中,一切都会正常。


对我来说,相关的文件甚至不在项目中了。我基本上遵循了这个建议,把它们复制回去。但是,它随后产生了关键编码符合性错误。我从重新添加的视图中删除了每个插座,然后它就工作了。 - public static void
2
即使你在Xcode内部重命名,这种情况仍会发生。Xcode 5.x真的很糟糕。 - Adam
这只是前几天发生在我身上的事情,我收到了NSInternalInconsistencyException错误并且无法加载bundle中的NIB文件。只有通过遵循Daniel C.的建议删除引用然后重新放置才能解决它。 - Daniel Zhang
对我没用。我正在尝试重构我的代码。这根本没有任何意义,我甚至找不到旧文件位置在哪里提到,但它就是找不到它们。 - Minsheng Liu
啊哈,我忘记了相同文件名的限制... - Minsheng Liu

18

只需要在“initWithNibName:”函数中从nib名称中删除“.xib”即可。 根据文档,“.xib”已被默认使用,不应该再使用。


16

我认为这个错误意味着在你的项目中找不到名为"RootViewController"的nib文件。

您正在编写以下代码:

self.viewController = [[RootViewController alloc]      initWithNibName:@"RootViewController_iPhone.xib" bundle:nil];

self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPad.xib" bundle:nil];

同时你要求它加载一个名为"RootviewController"的nib文件..!! 它在哪里..? 你有没有一个名为"Rootviewcontroller"的xib文件..?


2
是的,我有一个名为RootViewController的nib文件。 - Ortensia C.
你的nib文件包含在项目结构中吗?我的意思是,它是否在项目导航器中可见?如果是的话,尝试删除它(在删除时使用“仅删除引用”),然后从Finder中再次将其拖到Xcode中。清理并构建它。 - akshaynhegde
7
还有一件事,确保你的Nib文件在“Build Phases”下的“Copy Bundle Resources”中。 - akshaynhegde
好的,还有一件事,要仔细检查nib文件的拼写...因为有时候最简单的事情我们也会忽略掉..:p - akshaynhegde
您不需要指定.xib扩展名。如果这样做,它将无法被找到。 - jcpennypincher
通常情况下,当 NIB 文件在项目结构中但我忘记将其添加到目标时,我会遇到此错误。 - Hahnemann

14

对我来说,

我没有在所需目标中安装该nib。

步骤1:在项目导航器中单击您的nib(即第一个左选项卡)

步骤2:进入文件检查器(即第一个右选项卡)

步骤3:进入目标成员资格并在要使用nib的目标上选择。


已解决应用程序崩溃问题。 - suhas

10

我有一些子项目。

我通过将xib文件添加到项目的“复制包资源”构建阶段来解决了这个问题。


10

我已经遇到了很多类似的问题。对于我自己而言,我发现我链接的xib文件总是拼写错误。

在我的情况下,以下是导致异常的代码行:

*NSArray nib = [[NSBundle mainBundle] loadNibNamed:@"shelfcell" owner:self options:nil];

"shelfcell"是我的xib文件名。但我将其错拼为"ShelfCell"、"shelfCell"等,这导致了异常。所以不要过度纠结,检查代码和拼写即可。谢谢


这帮助了我解决问题。在我的情况下,在Swift中,我尝试使用相同的静态引用nibName和重用标识符,并且我输入的重用标识符有误。 - David M
这很遗憾是我的情况。感谢您的帮助! - kinezu

9
如果更改你的xib文件的名称并且忘记更改所有引用它的位置,这种情况也可能发生。
例如:MainWindow.xib是一个包含自定义ViewController FirstViewController和相关xib文件FirstView的TabBarController。你将其更改为SecondViewController,并关联xib文件SecondView。
解决方法:
打开包含TabBarController的MainWindow.xib:
- 选择已更改的ViewController对应的选项卡。 - 在Identity inspector中,更改Custom Class名称为SecondViewController。 - 在Attributes inspector中,更改xib名称为SecondView。
打开SecondView.xib(这是从FirstView.xib复制的):
- 在Identity Inspector中,将custom class更改为SecondViewController。
可能还需要:
- 右键单击SecondView.xib,验证已启用"Identity and Type-> Target Membership"。 - 确保"Build Phases -> Copy Bundle Resources"显示你的SecondView.xib。 - 我还发现我偶尔需要清理并从模拟器中删除应用程序才能使xib文件实际更新。模拟器有一个内部缓存xib文件的过程,有点不一致。

请确保“Build Phases -> Copy Bundle Resources”中显示了您的SecondView.xib。这个!这是导致我的应用程序崩溃的原因...非常感谢! - mtet88

5

出现这个错误可能有很多原因。我也遇到过一个并解决了它。

可能是你添加了第三方框架,但没有将其包含在“复制捆绑资源”中。这对我来说解决了问题。

按照以下步骤进行操作。转到目标 -> BuildPhases -> CopyBundleResources -> 拖放你的框架并运行代码。


1
我曾经重命名过一个xib,然后遇到了这个问题。对我来说,解决方法是手动编辑main window.xib并更改有问题的文本。
在相关的xib中(以源代码视图查看),搜索有问题的xib名称。
更改。
<viewController nibName="OldNibName" id="116" customClass="SomeViewController">

使用

<viewController nibName="NewNibName" id="116" customClass="SomeViewController">

并且它成功了


1

enter image description here

在身份检查器中,将类名更改为相应的标识符,并在属性检查器中修改标识符,然后它应该按预期工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接