我正在尝试根据运行时的某些条件从子文件夹加载 NiB 文件。问题是,当我调用方法 pathForResource 时,代码能够找到 NIB 文件,但是当我运行应用程序时,我总是收到以下错误
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在 bundle 中加载 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;
}
最佳答案
您的 NIB/XIB 应包含在您的 xCode 项目中。它们应该位于同一个文件夹中。您只能加载导入到您的项目中的NIB/XIB。
关于ios - 如何从不同的文件夹加载nib文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8177013/