本文介绍了关于Xcode 4中的通用应用程序的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:为iPhone创建:

Step 1: Create for iPhone:


  • 新文件/ ios / cocoa touch / UIViewController子类

  • 取消选中针对iPad的目标

  • 使用XIB查看

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

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

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

Step 2: Create new XIB for iPad:


  • 新文件/ ios /用户界面/查看
    设备系列iPad

  • 为方便起见选择在此xib中使用后缀_iPad(例如CustomView_iPad)

  • 的相同名称转到文件所有者,在检查器选项卡中选择身份检查器,自定义类并选择在步骤中创建的同一名称类1。

  • 连接IBOutlets。

  • New file / ios / user interface / viewdevice 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];
}

祝你好运!

这篇关于关于Xcode 4中的通用应用程序的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:09