iOS學習之控制器的建立,ios控制器建立

來源:互聯網
上載者:User

iOS學習之控制器的建立,ios控制器建立

本次部落格是一篇總結性質的部落格,總結的是各種建立控制器的方式以及一些需要注意的操作。

1、通過storyboard建立控制器

 正如我上一篇部落格中所說,當 Main Interface 沒有選定的時候,我們一般只能通過代碼來建立一個 UIWindow,不再使用系統建立好的 Main.storyboard。

 通過 storyboard 建立控制器也是在 Main Interface 沒有選定的時候,但是我們還使用系統建立好的 Main.storyboard,通過不同的方法來建立視窗的根控制器。

  • 第一步:在 AppDelegate.m 檔案的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中先初始化一個 UIWindow 對象,代碼如下:
    // 建立視窗    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  • 第二步:建立一個 UIStoryboard 對象並將其初始化為 Main.storyboard

  初始化方法:

// 參數1:storyboard檔案名稱,不需要帶尾碼// 參數2:置為nil時表示[NSBundle mainBundle]+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(nullable NSBundle *)storyboardBundleOrNil;

  執行個體代碼:

    // 載入storyboard    // storyboard檔案名稱,不需要帶尾碼,初始化為 Main.storyboard,name就是Main    // nil:  [NSBundle mainBundle]    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  • 第三步:通過storyboard建立控制器

  ① 指定箭頭指向的控制器為視窗的根控制器

  方法:

// 載入箭頭指向的控制器- (nullable __kindof UIViewController *)instantiateInitialViewController;

  執行個體代碼:

    // 通過storyboard建立控制器    // instantiateInitialViewController:載入箭頭指向的控制器    UIViewController *vc = [storyboard instantiateInitialViewController];

   執行個體圖:

  如:我們將粉色背景的控制器設定為預設 Main.storyboard 的控制器,當我們使用上述代碼時,程式啟動並執行效果是:如的一個粉色視窗

  ② 通過 Storyboard ID 來建立根控制器

  方法:

// 參數為:Storyboard ID 的字串- (__kindof UIViewController *)instantiateViewControllerWithIdentifier:(NSString *)identifier;

  執行個體代碼:

// 通過 Storyboard ID 建立控制器UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"green"];

  正如 ① 中所示的圖,我們將綠色背景的控制器的 Storyboard ID 置為 "green",但是Main.storyboard 的預設控制器仍然為粉色背景的控制器,使用上述執行個體代碼,運行結果為如的一個綠色視窗

  具體如何設定視圖控制器為 UIWindow 對象的根視圖控制器以及將 UIWindow 對象作為主視窗並可見,請參考上一篇部落格。

2、通過xib建立控制器

 通過 xib 建立控制器也是在 Main Interface 沒有選定的時候,自己在 AppDelegate.m 檔案中對應的方法中先初始化一個 UIWindow 對象。

 建立xib的方法:

  ① 在建立一個視圖控制器的類的時候,將 Also create XIB file 選中之後,就會建立出來一個和控制器類對應的xib,如:

    這種方法建立的xib的名字與所對應的類名相同。

  ② 建立一個名字可以自訂的 xib

  建立後的設定:(具體如)

   (1)選擇檔案所有者選項

   (2)綁定控制器

   (3)綁定xib視圖

  通過xib建立根控制器的方法是一個控制器的初始化方法,執行個體代碼:

    // 通過xib建立控制器    ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

  載入xib的特殊寫法:

    // 特殊寫法1:    UIViewController *vc = [[ViewController alloc] initWithNibName:nil bundle:nil];    // 特殊寫法2:    UIViewController *vc = [[ViewController alloc] init];

  以上 xib 的特殊寫法載入 xib View 視圖的底層實現:

   如果描述控制器 View 的 xib 跟控制器的類名相同,就會去載入

   只有控制器的 init 方法底層會調用 initWithNibName:bundle:

   只要通過 initWithNibName:bundle: 初始化控制器,並且 nibName 為 nil ,就會執行以下幾步:

   (1)先尋找有沒有跟控制器類名同名但是不帶Controller的xib,如果有就會去載入(XMGView.xib)

   (2)如果沒有就尋找有沒有跟控制器類名同名的xib,如果有就會去載入(XMGViewController.xib)

   (3)如果都沒有找到,建立空的view,

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.