iOS.Lover
iOS.Lover

Reputation: 6051

Questions about Universal application in Xcode 4

I am trying to build an universal application with Xcode 4. However, it seems a little different from past versions.

My project utilizes the View Based Application template. My issue is that I added a UIViewController subclass which has one nib file for iPad. How do I create another nib file with the same class targeted instead for iPhone? Also, how do I ensure that the proper nib file is loaded for the proper platform?

EDITED : here is my code :

- (IBAction)BookView {

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        BookViewController *viewController = [[BookViewController alloc] initWithNibName:@"BookViewController" bundle:nil];
        [self presentModalViewController:viewController animated:YES];

    } else {

        BookViewController *viewController = [[BookViewController alloc] initWithNibName:@"BookViewController_iPad" bundle:nil];
        [self presentModalViewController:viewController animated:YES];
    }
}

Upvotes: 2

Views: 1226

Answers (2)

Vlad Grigorov
Vlad Grigorov

Reputation: 11376

Name your xib file for iPad BookViewController~ipad.xib and the iPhone one BookViewController~iphone.xib. Then load the nib file as usual:

BookViewController *viewController = [[BookViewController alloc] initWithNibName:@"BookViewController" bundle:nil];
[self presentModalViewController:viewController animated:YES];

When the app runs on an iPad device, the ~ipad xib will be automatically loaded. If the app runs on an iPhone, the ~iphone xib will be automatically loaded.

Note that ~ipad and ~iphone suffixes are case sensitive. If you name it ~iPad for instance, you'll get a runtime exception that the nib file is not found.

Upvotes: 3

Roberson Balbino
Roberson Balbino

Reputation: 397

Step 1: Create for iPhone:

  • New file / ios / cocoa touch / UIViewController subclass
  • uncheck Targeted for iPad
  • check with XIB

This step will create .m .h and .xib files with same name, for example: CustomView

Step 2: Create new XIB for iPad:

  • New file / ios / user interface / view device family iPad
  • for convenience choose the same name with suffix _iPad (for example CustomView_iPad)
  • in this xib go to File's Owner, in the inspector tabs choose identity inspector, custom class and choose the same name of class created in step 1.
  • Connect IBOutlets.

In your code use something like this:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    CustomView *viewController = [[CustomView alloc] initWithNibName:@"CustomView" bundle:nil];
} else {
    CustomView *viewController = [[CustomView alloc] initWithNibName:@"CustomView_iPad" bundle:nil];
}

Good luck!

Upvotes: 6

Related Questions