天天看點

UIWindow 詳解

UIWindow對象是所有UIView的根視圖,管理和協調的應用程式的顯示、分發事件給View。UIWindow類是UIView的子類,可以看作是特殊的UIView。一般應用程式隻有一個UIWindow對象,即使有多個UIWindow對象,也隻有一個UIWindow可以接受到使用者的觸屏事件。UIWindow初始化在appDeleDgate裡面的 didFinishLaunchingWithOptions方法。

第一、UIWindow的建立

iPhone應用程式通常隻有一個UIWindow類的執行個體,該執行個體的建立如果是從nib檔案建立,則通常有個屬性變量,如果是用代碼建立,則必須在建立時傳入螢幕矩形,螢幕矩形可以通過UIScreen對象來取得,具體代碼如下所示:

self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]] autorelease];

第二、UIWindow的作用:

1.UIWindow作為一個容器,盛放所有的UIView

2.UIWindow會把其他其他所有消息傳遞給view

第三、常用的加載view的方法

1、addSubview

直接将view通過addSubview方式添加到window中,程式負責維護view的生命周期以及重新整理,但是并不會為去理會view對應的ViewController,是以采用這種方法将view添加到window以後,我們還要保持view對應的ViewController的有效性,不能過早釋放。

sample:

self.switchController=[[SwithcViewController alloc] initWithNibName:@"switchView" bundle:nil];
    UIView *switchView=self.switchController.view;
    CGRect switchViewFrame=switchView.frame;
    switchViewFrame.origin.y+=[UIApplication sharedApplication].statusBarFrame.size.height;
    switchView.frame=switchViewFrame;
    [self.window addSubview:switchView];
      

2、rootViewController

self.viewController = [[[AndyViewController alloc] initWithNibName:@"AndyViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
      

把建立的viewcontrolle付給rootViewController,,UIWindow将會自動将其view添加到目前window中,同時負責ViewController和view的生命周期的維護,防止其過早釋放.

官方解釋:

rootViewController

The root view controller for the window.

@property(nonatomic, retain) UIViewController *rootViewController

Discussion

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

The default value of this property is nil.

 第四、常用屬性分析:

1、windowLevel

UIWindow有三個層級,分别是Normal,StatusBar,Alert,如下所示:

UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;

UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;

UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar;

列印輸出他們三個這三個層級的值我們發現從左到右依次是0,1000,2000,也就是說Normal級别是最低的,StatusBar處于中等水準,Alert級别最高。而通常我們的程式的界面都是處于Normal這個級别上的,系統頂部的狀态欄應該是處于StatusBar級别,UIActionSheet和UIAlertView這些通常都是用來中斷正常流程,提醒使用者等操作,是以位于Alert級别。

根據window顯示級别優先的原則,級别高的會顯示在上面,級别低的在下面,我們程式正常顯示的view位于最底層,

官方解釋:

The receiver’s window level.

@property(nonatomic) UIWindowLevel windowLevel

Levels are ordered so that each level groups windows within it in front of those in all preceding groups. For example, alert windows appear in front of all normal-level windows. When a window enters a new level, it’s ordered in front of all its peers in that 

2.keyWindow

是唯一一個可以接受響應的Window,在一個應用程式中隻有唯一一個keyWindow

The key window is the one that is designated to receive keyboard and other non-touch related events. Only one window at a time may be the key window.

常用一下四個方法來操作

  • – makeKeyAndVisible
  • – becomeKeyWindow
  • – makeKeyWindow
  • – resignKeyWindow

繼續閱讀