xcode 无法在包中加载 NIB:'NSBundle 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18464012/
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
Could not load NIB in bundle: 'NSBundle ERROR
提问by user2721311
I have tried everything I have read on google, and StackOverflow but none of the suggestions seem to work. I have been stuck on this for 2 days trying to fix it but I hope you guys can help me out and suggest something I haven't tried...
我已经尝试了我在谷歌和 StackOverflow 上读到的所有内容,但似乎没有一个建议有效。我已经坚持了 2 天试图修复它,但我希望你们能帮助我并提出一些我没有尝试过的建议......
this is what I have tried and hasn't seemed to work:
这是我尝试过但似乎没有用的方法:
- restart mac
- restart Xcode
- restart simulator
- delete an app from the simulator
- the clean project then build then run
- change storyboard name (i changed it from ViewController to ViewController1 and as you see I still get the error
- made sure all the code with ViewController1 in it is spelled right and case sensitive
- made sure that storyboards are added to copy bundle in build phases
- deleted it from build phases and added it back
- made sure the storyboards weren't localized and vice versa
- made sure the project was added to the storyboard target in file inspector
- I tried disabling and enabling auto-layout both didn't work
- 重启mac
- 重启Xcode
- 重启模拟器
- 从模拟器中删除应用程序
- 干净的项目然后构建然后运行
- 更改故事板名称(我将其从 ViewController 更改为 ViewController1,如您所见,我仍然收到错误
- 确保其中包含 ViewController1 的所有代码拼写正确且区分大小写
- 确保在构建阶段将故事板添加到复制包
- 从构建阶段删除它并将其添加回来
- 确保故事板没有本地化,反之亦然
- 确保项目已添加到文件检查器中的故事板目标
- 我尝试禁用和启用自动布局都不起作用
NONE of these seemed to work I still get this error below and it won't load my app on the simulator it just stops at the black loading screen
这些似乎都不起作用我仍然在下面收到此错误并且它不会在模拟器上加载我的应用程序它只是在黑色加载屏幕上停止
**2013-08-27 12:07:15.057 Project52[647:11303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/Deondrae/Library/Application Support/iPhone Simulator/6.1/Applications/A350F56C-CC1C-462A-8D7C-63F2CB037EBC/Project52.app> (loaded)' with name 'ViewController1''
*** First throw call stack:
(0x159b012 0x12a8e7e 0x159adeb 0x408ef9 0x2cd7e7 0x2cddc8 0x2cdff8 0x2ce232 0x21d3d5 0x21d76f 0x21d905 0x226917 0x2bb5 0x1ea157 0x1ea747 0x1eb94b 0x1fccb5 0x1fdbeb 0x1ef698 0x25fddf9 0x25fdad0 0x1510bf5 0x1510962 0x1541bb6 0x1540f44 0x1540e1b 0x1eb17a 0x1ecffc 0x56ad 0x28d5)
libc++abi.dylib: terminate called throwing an exception
(lldb)**
I suspect that this error is being caused because of this code below... the code looks to load an xib but this is xcode 4.6 so we are using storyboards... what can i change the code to try make it work/load storyboards instead of the dated xib ?
我怀疑这个错误是由于下面的代码引起的......代码看起来加载了一个xib,但这是xcode 4.6所以我们正在使用故事板......我可以更改代码以尝试使其工作/加载故事板而不是过时的 xib ?
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
UPDATE: this is what i have now in my appdelegate.m (After trying suggestions) but i still get the error:
更新:这是我现在在我的 appdelegate.m 中的内容(尝试建议后),但我仍然收到错误:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[UIStoryboard storyboardWithName:@"ViewController1" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController1"];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
UPDATE 2:i still get the error ('NSInvalidArgumentException', reason: 'Could not find a storyboard named 'ViewController1')
更新 2:我仍然收到错误(“NSInvalidArgumentException”,原因:“找不到名为“ViewController1”的故事板)
- My storyboard file name is storyboard.storyboard
- I only have one view controller within that storyboard.
- the view controller within has a custom class of ViewController1
- 我的故事板文件名是 storyboard.storyboard
- 我在那个故事板中只有一个视图控制器。
- 其中的视图控制器有一个自定义类 ViewController1
this is what I have in the appdelegate.h
这就是我在 appdelegate.h 中的内容
#import <UIKit/UIKit.h>
@class ViewController1;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic) UIViewController *viewController;
@property (nonatomic) UINavigationController *navigationController;
@property (nonatomic) UIStoryboard* storyboard;
@property (strong, nonatomic) UIWindow *window;
@end
This is what I have in the appdelegate.m
这就是我在 appdelegate.m 中的内容
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.storyboard = [UIStoryboard storyboardWithName:@"storyboard" bundle:nil];
self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:self.viewController];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
}
UPDATE 3:
更新 3:
using @calinchitu code from the comments within the appdelegate.h and .m file seemed to have got rid of the error... however now I get a warning specifically after I close bracket on the code he provided on the .m file which says "Warning: Control reaches end of non-void function" only way to skip past it is with a breakpoint enabled but the warning still shows... how do I get rid of this?
使用 appdelegate.h 和 .m 文件中注释中的 @calinchitu 代码似乎已经消除了错误......但是现在我在关闭他在 .m 文件中提供的代码的括号后特别收到警告“警告:控制到达非空函数的结尾”跳过它的唯一方法是启用断点但警告仍然显示......我该如何摆脱这个?
UPDATE 4:
更新 4:
Add return YES; at the end of the code that @calin Chitu provided in the comments and it should get rid of the warning. HIS ANSWER IS CORRECT AND WORKED FINE BUT I DONT HAVE ENOUGH POINTS TO MARK THE ANSWER SO ANYONE ELSE WHO COMES ACROSS THIS ERROR USE HIS CODE / SUGGESTION IT WORKS - DONT FORGET THE return YES;
添加返回YES; 在@calin Chitu 在评论中提供的代码的末尾,它应该消除警告。他的答案是正确的并且工作正常,但我没有足够的分数来标记答案,因此遇到此错误的其他人请使用他的代码/建议它有效 - 不要忘记返回 YES;
回答by Calin Chitu
If you want to initiate your storyboard from appdelegate:
如果你想从 appdelegate 启动你的故事板:
at appdelegate.h
在 appdelegate.h
@property (nonatomic) UIViewController *viewController;
@property (nonatomic) UINavigationController *navigationController;
@property (nonatomic) UIStoryboard* storyboard;
@property (strong, nonatomic) UIWindow *window;
at appdelegate.m
在 appdelegate.m
self.storyboard = [UIStoryboard storyboardWithName:@"yourstoryboardname" bundle:nil];
self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:self.viewController];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
make sure in storyboard you have set StoryboardId as ViewController1 for your viewcontroller.
确保在故事板中,您已将 StoryboardId 设置为 ViewController1 的视图控制器。
You can also skip the entire code above, by going to your project settings, summary, and set Main Storyboard with the storyboard you've created.
您还可以通过转到项目设置、摘要并使用您创建的故事板设置主故事板来跳过上面的整个代码。
回答by Hermann Klecker
You may got confused between using storyboard and using individual nib files. Do you have a ViewController1.xib at all? As you are telling about including the storyboard to the boundle I assume that ViewController1 is part of your (or one of yours) storyboard. If so then go for:
您可能会对使用情节提要和使用单独的 nib 文件感到困惑。你有 ViewController1.xib 吗?当您谈到将故事板包含到边界时,我假设 ViewController1 是您的(或您的)故事板的一部分。如果是这样,那么去:
[self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
As you are in app delegate and not in a view controller use:
当您在应用程序委托中而不是在视图控制器中时,请使用:
self.viewController = [[UIStoryboard storyboardWithName:@"yourstoryboardname" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController1"];