I'm prototyping an app and I'd like to have multiple .nib layout files to let friends try out different layouts to see what works the best for them.
I already have code to properly switch between a hardcoded array of known .nib files but I'd like to expand my knowledge overall so thought I'd just try to write a general system that can find all .nib files in the bundle so I don't have to change any code if I add a new .nib layout.
I see that the paths for a given resource is really the full path. Something like:
/var/mobile/Applications/69B85DBB-3B79-43F0-B88E-FFFF/TestApp.app/en.lproj/some_asset.nib
I tried loading a .nib using the full path name and it doesn't work as far as I can tell. The only thing that seems to work is stripping off the path and the extension to just get down to the resource name?
If so, then I have the following:
NSArray *availableNibs = [[NSBundle mainBundle] pathsForResourcesOfType:@"nib"
inDirectory:nil];
// Just grab the first .nib as a test and remove the .nib extension
NSString *aNib = [availableNibs[0] stringByReplacingOccurrencesOfString:@".nib"
withString:@""];
// Start at the end of the string and find the first '/'
for (NSInteger index = [aNib length] - 1; index >= 0; --index) {
if ([aNib characterAtIndex:index] == '/' ) {
// Get us down to the resource name - return string starting after that '/'
aNib = [aNib substringFromIndex:index + 1];
break;
}
}
// have resource name in 'aNib', so open
self.viewController = [[My_ViewController alloc] initWithNibName:aNib
bundle:nil];
Maybe my expectations were wrong but this seems clumsier than I had expected? Is there an easier way to extract the resource names, maybe using less code?
And generally, are there any other pitfalls I might be aware of with trying to do this sort of thing or is this roughly the right approach?
Cheers in advance