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

Adr*_*lli 45 compiler-errors nib ios appdelegate

在我的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) 
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

这是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;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

小智 103

重命名XCode之外的文件时,可能会发生此错误.要解决此问题,您只需从项目中删除文件(右键单击 - 删除和"删除参考").

然后,您可以重新导入项目中的文件,一切都会好的.

  • 为什么这是第2部分问题.谢谢,截至2017年仍然相关 (17认同)
  • 为什么是这个问题第3部分.谢谢,截至2018年仍然相关 (15认同)
  • 为什么这是一个问题.谢谢,截至2015年仍然相关 (14认同)
  • 为什么是这个问题第5部分.谢谢,截至2018年5月仍然相关(3) (14认同)
  • 为什么这是第10部分.感谢,截至2019年1月仍然相关(7) (12认同)
  • 为什么是这个问题第4部分.谢谢,截至2018年仍然相关(2) (7认同)
  • 为什么这是第11部分,谢谢,截至2019年3月3日仍然有意义(10) (6认同)
  • 为什么是这个问题第7部分.谢谢,截至2018年7月24日仍然相关(5) (5认同)
  • 为什么这是第9部分.感谢,截至2018年12月仍然相关 (5认同)
  • 为什么这是问题的第六部分,谢谢,截至2018年7月仍然有意义(4) (4认同)
  • 即使您在 Xcode 中重命名,也会发生这种情况。Xcode 5.x 确实很糟糕。 (2认同)

pai*_*ego 18

只需尝试从"initWithNibName:"中的nib名称中删除".xib".根据文档,假定".xib",不应使用.xib.


aks*_*gde 17

好吧,我认为错误说它在你的项目中找不到名为"RootViewController"的nib文件.

你正在编写这些代码行,

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

self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPad.xib" bundle:nil];
Run Code Online (Sandbox Code Playgroud)

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

  • 还有一件事,确保您的nib文件存在于"构建阶段"下的"复制包资源"中. (6认同)
  • 是的,我有一个名为RootViewController的nib文件 (2认同)

小智 10

我经历了很多次同样的问题.我为自己找到的是我链接的xib文件总是拼写错误.就我而言,这是导致异常的代码行:

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

"shelfcell"是我的xib文件名.但我错误拼写为"ShelfCell","shelfCell"等,这导致异常.所以不要误会你的头脑.检查代码行和拼写.谢谢


kas*_*tus 9

我有一些子项目.

我通过将xib文件添加到项目的Copy Bundle Resources构建阶段来解决了这个问题.


ari*_*orf 8

如果更改xib文件的名称,并且忘记更改所引用的所有位置,也会发生这种情况.

示例:MainWindow.xib是一个TabBarController,它包含一个名为FirstViewController的自定义ViewController,其中包含相关的xib文件FirstView.您将名称更改为SecondViewController和关联的xib文件到SecondView.

解:

打开MainWindow.xib(包含TabBarController):

  • 选择您更改的ViewController的选项卡.
  • 在标识检查器上,将自定义类名称更改为SecondViewController.
  • 在属性检查器上,将xib名称更改为SecondView.

打开SecondView.xib(这是从FirstView.xib复制的):

  • 在Identity Inspector上,将自定义类更改为SecondViewController.

您可以选择:

  • 右键单击SecondView.xib,验证是否启用了"身份和类型 - >目标成员身份".
  • 确保"Build Phases - > Copy Bundle Resources"显示您的SecondView.xib
  • 我还发现我偶尔需要构建干净并从模拟器中删除应用程序以使xib文件实际更新.模拟器有一个xib文件的内部缓存,有点不一致.


Zai*_*han 8

为了我,

我没有在所需目标中安装该笔尖.

第1步:在Project Navigator中单击您的笔尖(即在第一个左侧选项卡中)

第2步:转到文件检查器(即在第一个右侧选项卡中)

第3步:转到目标成员身份并勾选您要使用笔尖的目标.


Imr*_*ran 5

导致这个错误的原因有很多,我也找到了一个并解决了。

您可能正在添加第三方框架,但未将其包含在复制捆绑资源中。这为我解决了问题。

请按如下方式进行操作。转到 Target -> BuildPhases -> CopyBundleResources -> 拖放框架并运行代码。