关于Xcode 4中的通用应用程序的问题

Mc.*_*ver 2 iphone sdk xcode ipad ios5

我正在尝试使用Xcode 4构建一个通用应用程序.但是,它似乎与过去的版本有点不同.

我的项目使用基于视图的应用程序模板.我的问题是我添加了一个UIViewController子类,它有一个用于iPad的nib文件.如何为iPhone创建另一个具有相同类的nib文件?另外,如何确保为正确的平台加载正确的nib文件?

编辑:这是我的代码:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*ino 6

第1步:为iPhone创建:

  • 新文件/ ios/cocoa touch/UIViewController子类
  • 取消选中针对iPad的目标
  • 用XIB查看

此步骤将创建具有相同名称的.m .h和.xib文件,例如:CustomView

第2步:为iPad创建新的XIB:

  • 新文件/ ios /用户界面/查看设备系列iPad
  • 为方便起见,选择带有后缀_iPad的相同名称(例如CustomView_iPad)
  • 在此xib中,转到文件所有者,在检查器选项卡中选择身份检查器,自定义类并选择在步骤1中创建的同一名称类.
  • 连接IBOutlets.

在你的代码中使用这样的东西:

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

祝好运!