Questions about Universal application in Xcode 4

岁酱吖の 提交于 2019-12-04 16:42:00

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!

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.

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