iOS – 通用应用程序
简述
通用应用程序是在单个二进制文件中为 iPhone 和 iPad 设计的应用程序。通用应用程序允许代码重用和快速更新。
通用应用 - 涉及的步骤
步骤 1 - 创建一个简单的 View based application.
步骤 2 - 更改文件名 ViewController.xib 归档到 ViewController_iPhone.xib 如下图右侧的文件检查器所示。

步骤 3 - 选择文件→新建→文件...然后选择小节 "User Interface" 并选择 View. 点击下一步。

步骤 4 − 选择设备系列为 iPad 然后点击下一步。

步骤 5 − 将文件另存为 ViewController_iPad.xib 并选择创建。
步骤 6 - 在屏幕中央添加一个标签 ViewController_iPhone.xib 和 ViewController_iPad.xib.
步骤 7 - 在 ViewController_iPad.xib,选择 identity inspector 并将自定义类设置为 ViewController.

步骤 8 - 更新 AppDelegate.m 中的 application:DidFinishLaunching:withOptions 方法如下 -
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];
// Override point for customization after application launch.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc]
initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:
@"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
步骤 9 − 将项目摘要中的设备更新为 Universal 如下图所示 -

输出
当我们运行应用程序时,我们将获得以下输出 -

当我们在 iPad 模拟器中运行应用程序时,我们将得到以下输出 -
