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;
}
精彩评论