开发者

Loading the Different Nib Files

I created two nib files for the iPhone and iPad, so my app will be universal.

I use this method开发者_StackOverflow社区 to check if it is an iPad:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

but I don't know how to load the proper nib when it knows which it is.

Does anyone know the correct method to load to nib file, accordingly?


Actually, Apple does all this automatically, just name your NIB files:

MyViewController~iphone.xib // iPhone
MyViewController~ipad.xib // iPad

and load your view controller with the smallest amount of code:

[[MyViewController alloc] initWithNibName:nil bundle:nil]; // Apple will take care of everything


Your interface files should be named differently so something like this should work.

UIViewController *someViewController = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    someViewController = [[UIViewController alloc] initWithNibName:@"SomeView_iPad" bundle:nil];
}
else
{
    someViewController = [[UIViewController alloc] initWithNibName:@"SomeView" bundle:nil];
}


You should use a initializer -[UIViewController initWithNibNamed:bundle:];. In your SomeViewController.m:

- (id)init {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (nil != (self = [super initWithNibName:@"SomeViewControllerIPad"])) {
            [self setup];
        }
    } else {
        if (nil != (self = [super initWithNibName:@"SomeViewControllerIPhone"])) {
            [self setup];
        }
    }
    return self;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜