我正在尝试将 NIB/XIB 文件放入一个名为 Configuration.bundle 的包中。当我尝试从我的包中加载 xib 时,应用程序崩溃,因为它找不到 xib 文件。
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]
pathForResource:@"Configuration" ofType:@"bundle"]];
[bundle load];
NSLog(@"bundle: %@", bundle);
我得到输出
bundle: NSBundle (not yet loaded)
“尚未加载”部分让我有点害怕。为什么没有加载?
最后当我尝试用 View Controller 加载我的 Nib 时
ConfigViewController *configViewController = [[ConfigViewController alloc] initWithNibName:@"ConfigViewController" bundle:bundle];
我得到输出
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (not yet loaded)' with name 'ConfigViewController.xib'
我已经尝试过使用和不使用 .xib。
有什么想法吗?
最佳答案
load方法是用来加载框架之类的可执行代码的。
你的包不包含任何你想加载的可执行文件,所以你不需要调用[bundle load]。
如果 bundle 的名称是正确的,那么除了 [bundle load] 之外你写的所有内容都可以。无论如何检查路径是否正确,不要嵌套太多指令:
NSString* path=[ [NSBundle mainBundle] pathForResource: @"Configuration" ofType: @"bundle"];
如果此字符串是正确的路径,那么您可以确定无需调用 [bundle load] 即可正确加载包。
关于objective-c - 从我自己的包中加载 Nib ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13825531/