通用应用程序是为iPhone和iPad设计的单个二进制文件中的应用程序。通用应用程序允许代码重用和快速更新。
涉及步骤
步骤1 - 创建一个简单的基于视图的应用程序。
步骤2 - 如下图所示,将文件名 ViewController.xib 文件更改为 ViewController_iPhone.xib 。
![iOS Tutorial](http://dev-img.mos.moduyun.com/20231017/14938baf-602d-4913-9c8b-5547d7da97bb.jpg)
步骤3 - 选择"File"→"New"→"File...",然后选择"User Interface"子部分,然后选择" View" 。点击下一步。
![iOS Tutorial](http://dev-img.mos.moduyun.com/20231017/484711bd-6c0a-484c-bbc9-1f45b5293302.jpg)
步骤4 - 将设备系列选择为 iPad ,然后单击下一步。
![iOS Tutorial](http://dev-img.mos.moduyun.com/20231017/5bfb750a-c47d-4f35-ac57-ae271b84eb8b.jpg)
步骤5 - 将文件另存为 ViewController_iPad.xib ,然后选择创建。
步骤6 - 在屏幕的中心分别在 ViewController_iPhone.xib 和 ViewController_iPad.xib 中添加标签。
步骤7 - 在 ViewController_iPad.xib 中,选择身份检查器,然后将自定义类设置为 ViewController 。
![iOS Tutorial](http://dev-img.mos.moduyun.com/20231017/a841f059-f332-4989-9667-c00d0b1bd587.jpg)
步骤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 ,如下所示-
![iOS Tutorial](http://dev-img.mos.moduyun.com/20231017/ef833dfe-a594-4016-9923-b065606fb99b.jpg)
运行应用程序时,将获得以下输出-
![iOS Tutorial](http://dev-img.mos.moduyun.com/20231017/e4c606b1-7273-46fc-b07e-5e6d2b91b5d7.jpg)
在iPad模拟器中运行应用程序时,将获得以下输出-
![iOS Tutorial](http://dev-img.mos.moduyun.com/20231017/8790a4e8-75ab-4b58-91ad-281df2f2ffcf.jpg)