Search code examples
iphoneobjective-ciosxcodexcode4

Multiple versions of .nib file


Is there any way to have two .nib files with the same name? I'm using Xcode 4, and I want to have multiple versions of interface i.e: MainMenu.xib in different folders, so I can access them with:

[[MainMenuViewController alloc] initWithNibName:@"MainMenu" bundle:skinBundle]

where my skinBundle would be bundle from path:

skinBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"skinFolderPath" ofType:nil]

When I build application, all directories are flattened resulting only one MainMenu.nib. When I add folders as a folder references, directory tree is preserved but none of the .xib files are compiled to .nib.

Is there any way to achieve this? Or maybe better way to manage multiple versions of nibs?


Solution

  • You can create additional targets in Xcode called "resource bundles." These have the bundle structure (like .app or .framework bundles) but contain no binary code. Add your resources to those bundles, add the products of those targets to your main target's "Copy Bundle Resources" stage, and then use those bundles during instantiation using code such as the following:

    NSString *skinBundlePath = [[NSBundle mainBundle] pathForResource: @"MyBundle" ofType: @"skin"]; // or whatever it's called
    if (skinBundlePath) {
        NSBundle *skinBundle = [NSBundle bundleWithPath: skinBundlePath];
    }
    

    The above snippet assumes you set the target's wrapper extension to "skin" and named it "MyBundle".

    In Xcode 3 (I don't have 4 handy), you can add a target by going to Project -> New Target. Pick "Loadable Bundle" from Mac OS X (even if you're using iOS.) Off the top of my head, you don't need any further configuration to get it working, but you must take care that resources wind up in the right bundle when you add them.

    If that sounds overly complicated, it's also possible to create a new project that builds a loadable bundle, and make that a dependency of your main project; again, add the build product of the subproject to your main project's resources "Copy Bundle Resources" stage.