Reputation: 6051
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
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
Reputation: 397
Step 1: Create for iPhone:
This step will create .m .h and .xib files with same name, for example: CustomView
Step 2: Create new XIB for iPad:
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