通用的應用程序是為iPhone和iPad在一個單一的二進制文件中設計的應用程序。這有助於代碼重用,並能夠幫助更快進行更新。
1、創建一個簡單的View based application(視圖應用程序)
2、在文件查看器的右邊,將文件ViewController.xib的文件名稱更改為ViewController_iPhone.xib,如下所示
3、選擇"File -> New -> File... ",然後選擇User Interface,再選擇View,單擊下一步
4、選擇iPad作為設備,單擊下一步:
5、將該文件另存為ViewController_iPad.xib,然後選擇創建
6、在ViewController_iPhone.xib和ViewController_iPad.xibd的屏幕中心添加標簽
7、在ViewController_iPhone.xib中選擇identity inspector,設置custom class為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模擬器中運行應用程序,我們會得到下面的輸出: