ios NSInternalInconsistencyException',原因:'无法在包中加载 NIB:'NSBundle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12722461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle
提问by Adriana Carelli
In my AppDelegate there is a problem I do not understand. RootViewController initially called ViewController and I changed it name. The application is formed by many ViewController then I have introduced a UINavigationController. Why this error comes?
在我的 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)
This is the code in AppDelegate.h
这是 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
This is the code of AppDelegate.m
这是 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;
}
Thanks in advance
提前致谢
回答by Daniel C.
This error can occur when you rename files outside of XCode. To solve it you can just remove the files from your project (Right Click - Delete and "Remove Reference").
当您在 XCode 之外重命名文件时,可能会发生此错误。要解决它,您只需从项目中删除文件(右键单击 - 删除并“删除引用”)。
Then after you can re-import the files in your project and everything will be OK.
然后在您可以重新导入项目中的文件之后,一切都会好起来的。
回答by paiego
Simply try removing the ".xib" from the nib name in "initWithNibName:". According to the documentation, the ".xib" is assumed and shouldn't be used.
只需尝试从“initWithNibName:”中的笔尖名称中删除“.xib”即可。根据文档,“.xib”是假定的,不应使用。
回答by akshaynhegde
Well, I think the error says that it can't find a nib file named "RootViewController" in your project.
好吧,我认为错误表明它在您的项目中找不到名为“RootViewController”的 nib 文件。
You are writing these lines of code,
您正在编写这些代码行,
self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPhone.xib" bundle:nil];
self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPad.xib" bundle:nil];
At the same time you are asking it to load a nib file named "RootviewController"..!! Where is it..? Do you have a xib file named "Rootviewcontroller"..?
同时你要求它加载一个名为“RootviewController”的nib文件..!!它在哪里..?你有一个名为“Rootviewcontroller”的xib文件吗?
回答by Reeshna
I've gone through the same issue quite a number of times. What I've found for myself is that the xib file i'm linking would always be misspelled. In my case, this is the line of code for me that lead to the exception:
我已经经历过很多次同样的问题。我发现我自己链接的 xib 文件总是拼写错误。就我而言,这是导致异常的代码行:
*NSArray nib = [[NSBundle mainBundle] loadNibNamed:@"shelfcell" owner:self options:nil];
* NSArray nib = [[NSBundle mainBundle] loadNibNamed:@"shelfcell" owner:self options:nil];
The "shelfcell" was my xib file name. But i had mis spelled it as "ShelfCell", "shelfCell" etc, which lead to the exception. So dont bug your head much. Check the lines of code and the spellings. Thank you
“shelfcell”是我的xib 文件名。但是我错误地将其拼写为“ShelfCell”、“shelfCell”等,这导致了异常。所以不要太麻烦你的头。检查代码行和拼写。谢谢
回答by kaspartus
I've got some subprojects.
我有一些子项目。
I solved this problem via adding xib file to Copy Bundle Resources build phase of mainproject.
我通过将 xib 文件添加到主项目的Copy Bundle Resources 构建阶段解决了这个问题。
回答by Zaid Pathan
For me,
为了我,
I did not install that nib in required target.
我没有在所需的目标中安装那个笔尖。
Step 1:Click on your nib in Project Navigator (i.e. In First left tab)
第 1 步:在 Project Navigator 中单击您的笔尖(即在第一个左侧选项卡中)
Step 2:Go to File Inspector (i.e. In First right tab)
第 2 步:转到文件检查器(即在第一个右侧选项卡中)
Step 3:Go to Target membership and tick on target in which you want to use your nib.
第 3 步:转到 Target 会员资格并勾选您要使用笔尖的目标。
回答by arinmorf
This can also occur if you change the name of your xib file, and you forget to change all the places it is referenced.
如果您更改了 xib 文件的名称,并且忘记更改引用它的所有位置,也会发生这种情况。
Example: MainWindow.xib is a TabBarController that contains a custom ViewController named FirstViewController with associated xib file FirstView. You change the name to SecondViewController and associated xib file to SecondView.
示例:MainWindow.xib 是一个 TabBarController,它包含一个名为 FirstViewController 的自定义 ViewController,以及关联的 xib 文件 FirstView。您将名称更改为SecondViewController和关联XIB文件SecondView。
Solution:
解决方案:
Open MainWindow.xib (that contains the TabBarController):
打开 MainWindow.xib(包含 TabBarController):
- Select the tab that is for the ViewController you changed.
- On the Identity inspector, Change the Custom Class name to SecondViewController.
- On the attributes inspector, Change the xib name to SecondView.
- 选择与您更改的 ViewController 对应的选项卡。
- 在身份检查器上,将自定义类名称更改为 SecondViewController。
- 在属性检查器上,将 xib 名称更改为 SecondView。
Open SecondView.xib (this was copied from FirstView.xib):
打开 SecondView.xib(这是从 FirstView.xib 复制的):
- On the Identity Inspector, change the custom class to SecondViewController.
- 在 Identity Inspector 上,将自定义类更改为 SecondViewController。
Optionally you may need to:
(可选)您可能需要:
- Right click SecondView.xib, verify that "Identity and Type->Target Membership" is enabled.
- Make sure "Build Phases -> Copy Bundle Resources" shows your SecondView.xib
- I also found I occasionally needed to build clean and delete the app from the simulator to get the xib file to actually update. The simulator has an internal cache for xib files that is a little inconsistent.
- 右键单击 SecondView.xib,验证“身份和类型->目标成员资格”是否已启用。
- 确保“构建阶段-> 复制捆绑资源”显示您的 SecondView.xib
- 我还发现我偶尔需要从模拟器中构建干净并删除应用程序才能真正更新 xib 文件。模拟器有一个有点不一致的 xib 文件的内部缓存。
回答by Imran
There are so many reasons for this error.I also got one and solved it.
这个错误有很多原因。我也得到了一个并解决了它。
It may be possible that you are adding a third party framework and not including it in the Copy Bundle Resources.That solved the problem for me.
您可能正在添加第三方框架,但未将其包含在 Copy Bundle Resources 中。这为我解决了问题。
Do this as follows. Go to Target -> BuildPhases -> CopyBundleResources -> Drag and drop your framework and run the code.
执行以下操作。转到 Target -> BuildPhases -> CopyBundleResources -> 拖放您的框架并运行代码。
回答by gheese
I had renamed a xib and had this problem, the solution for me was to edit the main window.xib manually and change the offending text
我曾改名厦门国际银行,并有这个问题,我的解决办法是手工编辑为主window.xib,改变违规文本
search for the offending xib name in the relevant xib ( view as source code)
在相关的 xib 中搜索有问题的 xib 名称(查看源代码)
changed
改变了
<viewController nibName="OldNibName" id="116" customClass="SomeViewController">
with
和
<viewController nibName="NewNibName" id="116" customClass="SomeViewController">
and it worked
它起作用了
回答by Steven Delrue
Also make sure that the XIB-file is included in your target. Check the file inspector when the XIB-file is selected (first tab on the right, in the middle there is section "target membership". The target you build for must be checked, otherwise the XIB-file won't be included.
还要确保 XIB 文件包含在您的目标中。选中 XIB 文件时检查文件检查器(右侧第一个选项卡,中间有“目标成员资格”部分。必须检查您构建的目标,否则将不会包含 XIB 文件。