iOS通用應用程式


簡介

通用的應用程式是為iPhone和iPad在一個單一的二進位制檔案中設計的應用程式。這有助於程式碼重用,並能夠幫助更快進行更新。

例項步驟

1、建立一個簡單的View based application(檢視應用程式)

2、在檔案檢視器的右邊,將檔案ViewController.xib的檔名稱更改為ViewController_iPhone.xib,如下所示

UniversalAppInterfaceRename

3、選擇"File -> New -> File... ",然後選擇User Interface,再選擇View,單擊下一步

NewIpadXib

4、選擇iPad作為裝置,單擊下一步:

UniversalAppSelectDeviceType

5、將該檔案另存為ViewController_iPad.xib,然後選擇建立

6、在ViewController_iPhone.xib和ViewController_iPad.xibd的螢幕中心新增標籤

7、在ViewController_iPhone.xib中選擇identity inspector,設定custom class為ViewController

UniversalAppSetClass

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,如下所示:

UniversalAppSetDevices

輸出

執行該應用程式,我們會看到下面的輸出

UniversalAppiPhone_Output

在iPad模擬器中執行應用程式,我們會得到下面的輸出:

UniversalAppiPad_Output