简体   繁体   中英

Is it possible to have two xib files with the same name in one project with Xcode?

I have an iPhone project that I'm creating a free/lite version for. I've created a second target and at this point I'm trying to deal with the different interfaces that each target will have. Almost all views will be different between targets in the sense that they will at least have an adView at the top. However some views are different looking in several ways.

I'm wondering if it's possible to create two xibs with the same name for each view controller to load. The lite xib will go in its own directory and I'll only add it to the bundle for the lite target. Due to there being separate bundles I would think if the correct xib was in the correct bundle I wouldn't have to differentiate in code which xib I'd like to load.

I'm trying to do this with xcode 4.2 but xcode crashes when I try to open the second xib with the same name.

Thanks in advance for any help.

A better and more sensible way to do this would be to name give one xib file the full version name and the other xib file a lite name. EG "MyGreatAppView-Full.xib" and "MyGreatAppView-Lite.xib".

When you instantiate the view controller programatically, one of the parameters you can pass is the XIB name you want to load.

EG

MyGreatAppViewController * appVC;

#if LITEVERSION
appVC = [[MyGreatAppViewController alloc] initWithNibName: @"MyGreatAppView-Lite.xib" bundle: nil];
#else
appVC = [[MyGreatAppViewController alloc] initWithNibName: @"MyGreatAppView-Full.xib" bundle: nil];
#endif

The other idea you had in mind, to keep the xib files all named the same, depends on keeping the xib files in separate folders and hoping that XCode won't get confused. XCode 3 was pretty good about this. XCode 4, at least for me, is not the most stable beast.

Yes you can. When you create a new target it creates a new directory in your project folder which makes it easy to have the same file names for multiple files. When you add a new or old interface file, in fact any file, Xcode asks you which target you want to add. Just check the target where you want to add. You can manually manage files in Target/Build Phases/Bundle Resources

I have made a number of free version already this is what I usually do:

  1. Create a new Target for the free version

  2. Remove all its added files except appname-info.plist file.

  3. Add all common source files to the target Target/Build Phases/Compile Resources (can drag and drop from navigator)

  4. Add all common other files (icons etc.) to the target Target/Build Phases/Bundle Resources (can drag and drop from navigator)

  5. Add all required frameworks (can drag and drop from navigator)

  6. Left click/Show in Finder on the interface files I want to change. Copy files to the newly created directory in the project

  7. Drag the copied interface files from Finder to Xcode and drop it in the newly created target group. Uncheck "Copy items into the destination group" as it is already there. Make sure that the desired target is checked under "Add to targets"

It also works for everything else icons, images, videos, etc. If you avoid hardcoding names, it works like a charm. I have projects with up to 6 targets (3 full version, 3 free) which share the same code, but different xib, icon and multimedia files. I usually remove limitations based on whether the app is free/lite or not with the following code (Mac, for iOS just remove the badge setting lines) :

NSString *appName = [NSString stringWithString:[[[NSBundle mainBundle] infoDictionary]   objectForKey:@"CFBundleName"]];
if ([appName rangeOfString:@"Lite"].location != NSNotFound || [appName rangeOfString:@"lite"].location != NSNotFound) {
    //put lite badge on
    NSString *badgeText = @"lite";
    [[NSApp dockTile] setBadgeLabel:badgeText];
    fullVersion = NO;
} else if ([appName rangeOfString:@"Free"].location != NSNotFound || [appName rangeOfString:@"free"].location != NSNotFound) {
    //put free badge on
    NSString *badgeText = @"free";
    [[NSApp dockTile] setBadgeLabel:badgeText];
    fullVersion = NO;
} else {
    fullVersion = YES;
}

I don't think you can use the same name for different xib file. After you build the app, you will find all the xib files are compiled to nibs and place in the same directory(in the main bundle). But you can use different names and if-else will be helpful.


if(liteVersion){ viewControll = [[YourViewController alloc] initWithNibName: @"YourViewController-Lite.xib" bundle: nil]; }else{ viewControll = [[YourViewController alloc] initWithNibName: @"YourViewController.xib" bundle: nil]; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM