'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个回答

1
在我的情况下,这是因为我删除了 Main.storyboard 并且在一般选项卡中清除了 Main Interface 属性,并添加了自定义的属性 Start。在我将 Main Interface 更改为 Start.storyboard 除了 Start 之外,它就可以工作了。

1
您可以检查xib文件是否与您的文件类具有相同的名称。默认情况下,XCode将从文件类的名称获取xib文件的名称,因此如果它们的名称不同,XCode无法从故事板中检测到它们。 对我很有效。

1

同样确保XIB文件已被包括在您的目标中。当选择XIB文件时,请检查文件检查器(位于右侧的第一个选项卡,在中间有一个“目标成员资格”部分)。必须勾选您构建的目标,否则XIB文件将不会被包括。


1

我在我的集合视图控制器的init(coder:构造函数中设置了一个可选的self.collectionView?.backgroundColor。

一旦我将这个设置移动到viewDidLoad中,异常就不知怎么消失了。令人困惑。


1

我花了一个小时的时间找出问题所在...但是清理项目的操作解决了这个问题。

Build -> Clean All

被接受的解决方案可能也能工作,但对我来说这就足够了。


0

我曾经遇到过这个错误很多次,也研究了为什么会出现这个错误,最终,我捕捉到了这个错误,这是一个愚蠢的错误,当我们拼错.xib文件名时就会发生。

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

在这个时候,我将 TableViewCell 拼错成了 TabelViewCell,因为有时我们会错误地读取它,这是最常见的问题。 只需复制您的 .xib 文件名并粘贴即可。


0
请注意,如果您正在使用[self class] nibName],则在使用Swift时可能会返回不同的名称...可能与您期望返回的名称不同。
特别是如果您的代码有多个目标。
在这种情况下,请尝试覆盖:
override var nibName: String?{
        return "YourXibFileName" // No .xib at the end :)
}

0
在开发iOS应用程序时,我也遇到了类似的问题。简单地重启Xcode对我有帮助。希望这能帮助到某些人。

0

我尝试了几种方法,最终对我起作用的是删除(并移到回收站)相关的.xib文件。然后重新创建它。为了方便操作,我先将.xib文件中的内容暂时复制到另一个.xib文件中,然后再将其复制回新创建的文件中。


0

当我尝试将gestureRecognizer添加到我的ViewController的视图中,并且目标是视图而不是self时,我遇到了这个错误:

应该改为:

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(handleTap(_:))))

修正为:

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap(_:))))

错误已经消失了。


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