iosxibnibloadnibnamed

how to load nib files from different folders?


I'm trying to load NiB files from subfolders based on some condition at runtime. The problem is that the code is able to locate the NIB file when i call the method pathForResource but when i run the application i always get the following error

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle

Here's my code:

// 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;
}

Solution

  • Your NIB / XIB should be included in your xCode project. They should be in the same folder. You can only load NIB / XIB imported to your project.