如何从不同的文件夹加载nib文件?

时间:2011-11-18 02:19:30

标签: ios xib nib loadnibnamed

我正在尝试根据运行时的某些条件从子文件夹加载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;
}

1 个答案:

答案 0 :(得分:1)

您的NIB / XIB应该包含在您的xCode项目中。它们应该在同一个文件夹中。您只能加载导入项目的NIB / XIB。