iOS 中的UIWindow

來源:互聯網
上載者:User

標籤:

使用Xcode建立一個工程後,Xcode會自動建立一些檔案,其中有AppDelegate.h,AppDelegate.m,ViewController.h,ViewController.m,Main.storyboard。AppDelegate.h中有一個對象是UIWindow,代碼如下:

@property (strong, nonatomic) UIWindow *window;

那麼,這個window的作用是什麼呢?

實際上,一個app之所以能夠顯示在螢幕上,就是因為有該UIWindow對象。只有UIWindow能夠主動顯示,其他的view可以添加到UIWindow上,這樣,其他的view也得以顯示在螢幕上。

雖然在Xcode7中已經不能建立一個沒有storyboard的工程,但是我們可以通過修改程式的Main Interface來測試UIWindow。方法如下:

點擊工程->General,將Main Interface 改為空白。如:

這時候再運行程式,會發現整個螢幕黑屏,如:

 

黑屏的原因就是因為沒有實現UIWindow。

現在,手動實現下UIWindow。

 

在AppDelegate.m中添加window的代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}

此時運行程式會發現有錯誤,錯誤提示如下:

‘NSInternalInconsistencyException‘, reason: ‘Application windows are expected to have a root view controller at the end of application launch‘

提示沒有rootViewController。也就是說,需要設定UIWindow對象的rootViewController。

設定UIWindow的rootViewController代碼如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];        // 設定rootViewController    ViewController *controller = [[ViewController alloc] init];    self.window.rootViewController = controller;        return YES;}

 運行程式,會看到和沒有修改Main Interface 之前是一樣的。

我們知道,在app程式啟動後,會立刻調用 

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
方法,在該方法中可以設定 UIWindow和UIWindow 的 rootViewController。這樣,整個app就顯示在螢幕上。

在Xcode中建立工程時,通常情況下會包含 storyboard 檔案,此時是不需要在上面的方法中設定 UIWindow的。那麼,在有storyboard檔案的情況下,是如何設定UIWindow的呢?

實際上,程式在載入主要storyboard的時候(也就是Main Interface 指向的storyboard),就會設定UIWindow和UIWindow的rootViewController。

總結下一個app程式的啟動過程:

1: 調用 main 方法

2:調用 UIApplicationMain方法,在該方法中會做下面兩件事:

     (1)建立UIApplication對象

     (2)建立UIApplication的delegate對象

3.1 在沒有storyboard的情況下,delegate對象開始監聽系統事件:

  (1)程式啟動完畢的時候,就會調用 application:didFinishLaunchingWithOptions 方法

  (2)在 application:didFinishLaunchingWithOptions 方法中建立UIWindow

  (3)建立和設定UIWindow的rootViewController

  (4)顯示創庫

3.2 在有storyboard的情況下,根據Info.plist獲得最主要的storyboard的檔案名稱,載入最主要的storyboard:

  (1)建立UIWindow

  (2)建立和設定UIWindow的rootViewController

  (3)顯示視窗



iOS 中的UIWindow

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.