jylee
jylee

Reputation: 843

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 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?

Upvotes: 0

Views: 746

Answers (3)

Adam
Adam

Reputation: 33146

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

Upvotes: 1

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;
}

Upvotes: 0

Joe
Joe

Reputation: 57179

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];
}

Upvotes: 0

Related Questions