UIQWindow定義了一個window對象來管理views。一個軟件只能有一個window。window的主要職能使為view提供顯示取和向view傳遞事件。想要改變軟件顯示的內容,你可以改變window的root view。
UIWindow的screen屬性指定了window的顯示屬性包括:bounds, mode, and brightness.
window notifications用來監聽window 和 screen的改變,包括:
UIWindowDidBecomeVisibleNotification
UIWindowDidBecomeHiddenNotification
UIWindowDidBecomeKeyNotification
UIWindowDidResignKeyNotification
UIWindow繼承自UIView,關於這一點可能有點邏輯障礙,畫框怎麼繼承自畫布呢?不要過於去專牛角尖,畫框的形狀不就是跟畫布一樣嗎?拿一塊畫布然後用一些方法把它加強,是不是可以當一個畫框用呢?這也是為什麼 一個view可以直接加到另一個view上去的原因了。一個應用程序只能有一個畫框。
看一下系統的初始化過程(在application didFinishLauchingWithOptions裡面):
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; myViewController = [[MyViewController alloc] init]; window.rootViewController = myViewController; [window makeKeyAndVisible]; return YES; } 創建一個an external display: - (void)checkForExistingScreenAndInitializeIfPresent { if ([[UIScreen screens] count] > 1) { // Get the screen object that represents the external display. UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1]; // Get the screen's bounds so that you can create a window of the correct size. CGRect screenBounds = secondScreen.bounds; self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds]; self.secondWindow.screen = secondScreen; // Set up initial content to display... // Show the window. self.secondWindow.hidden = NO; } } - (void)setUpScreenConnectionNotificationHandlers { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(handleScreenDidConnectNotification:) name:UIScreenDidConnectNotification object:nil]; [center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:) name:UIScreenDidDisconnectNotification object:nil]; } - (void)handleScreenDidConnectNotification:(NSNotification*)aNotification { UIScreen *newScreen = [aNotification object]; CGRect screenBounds = newScreen.bounds; if (!self.secondWindow) { self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds]; self.secondWindow.screen = newScreen; // Set the initial UI for the window. } } - (void)handleScreenDidDisconnectNotification:(NSNotification*)aNotification { if (self.secondWindow) { // Hide and then delete the window. self.secondWindow.hidden = YES; self.secondWindow = nil; } }