iOS - 通用應用程式



通用應用程式是指為 iPhone 和 iPad 設計的,在一個二進位制檔案中執行的應用程式。通用應用程式允許程式碼重用並快速更新。

通用應用程式 – 涉及的步驟

步驟 1 − 建立一個簡單的基於檢視的應用程式

步驟 2 − 將檔名ViewController.xib檔案更改為ViewController_iPhone.xib,如下所示,在右側的檔案檢查器中。

iOS Tutorial

步驟 3 − 選擇檔案 → 新建 → 檔案... 然後選擇子部分“使用者介面”並選擇檢視。單擊下一步。

iOS Tutorial

步驟 4 − 選擇裝置系列為iPad,然後單擊下一步。

iOS Tutorial

步驟 5 − 將檔案儲存為ViewController_iPad.xib並選擇建立。

步驟 6 − 在ViewController_iPhone.xibViewController_iPad.xib中,在螢幕中央新增一個標籤。

步驟 7 − 在ViewController_iPad.xib中,選擇身份檢查器並將自定義類設定為ViewController

iOS Tutorial

步驟 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 − 將專案摘要中的裝置更新為通用,如下所示 −

iOS Tutorial

輸出

當我們執行應用程式時,我們將獲得以下輸出 −

iOS Tutorial

當我們在 iPad 模擬器中執行應用程式時,我們將獲得以下輸出 −

iOS Tutorial
廣告

© . All rights reserved.