Questions about Universal application in Xcode 4

折月煮酒 提交于 2019-12-06 11:06:32

问题


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

回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/8232099/questions-about-universal-application-in-xcode-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!