【发布时间】:2011-12-31 21:32:54
【问题描述】:
我正在尝试根据运行时的某些条件从子文件夹中加载 NiB 文件。问题是当我调用方法 pathForResource 时代码能够找到 NIB 文件,但是当我运行应用程序时,我总是收到以下错误
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在包中加载 NIB:'NSBundle
这是我的代码:
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if(somecondition == true)
{
NSString* nibPath = [[NSBundle mainBundle] pathForResource:nibNameOrNil ofType:@"xib" inDirectory:@"CAS"];
NSString* customPath = [nibPath stringByDeletingLastPathComponent];
NSBundle* localeBundle = [NSBundle bundleWithPath:customPath];
if (localeBundle!= nil) {
nibBundleOrNil = localeBundle;
}
}
}
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}
【问题讨论】:
-
它是 [NSBundle MainBundle];而且我认为您对 Xcode 中所谓的“文件夹”的结构感到困惑。当 Xcode 创建文件夹时,它只是编辑项目的 XML 以将这些文件包含在伪文件夹包中。它从不真正写入文件系统。
-
如果没有“文件夹”,那么“inDirectory”字段是什么?
-
这应该暗示你引用了你的包中的一个文件,比如 NSHomeDirectory 或 NSDocumentsDirectory。
标签: ios xib nib loadnibnamed