0

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;
}
Lena Schimmel
  • 7,203
  • 5
  • 43
  • 58
ArdenDev
  • 4,051
  • 5
  • 29
  • 50
  • It's [NSBundle MainBundle]; and I think you are confused as to the structure of the so called "folders" in Xcode. When Xcode creates a folder, it just edits your project's XML to include those files in a pseudo-folder bundle. It never actually writes to the file system. – CodaFi Nov 18 '11 at 04:08
  • so what is the 'inDirectory' field used if there aren't 'folders' ? – ArdenDev Nov 18 '11 at 17:54
  • It's supposed to imply that you make reference to a file in your bundle such as the NSHomeDirectory or the NSDocumentsDirectory. – CodaFi Nov 18 '11 at 23:21

1 Answers1

1

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.

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • I've organized my xcode project using files from the file system and some XIb's are in subfolders and I've also added them to my xcode project. – ArdenDev Nov 18 '11 at 17:14
  • In your xCode project , even the XIB is in a `group` (folder-like icon in File Explorer in xCode), they are treated as the same level of root. Just call the XIB name to load the file. – Raptor Nov 21 '11 at 03:53