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

sample:

    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

Discussion

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

UIWindow 详解的更多相关文章

  1. UIWindow详解

    UIScreen(屏幕),UIWindow(窗口),UIView(视图)是iOS的几个基本界面元素.其中UIWindow(窗口)和UIView(视图)是为iPhone应用程序构造用户界面的可视组件.U ...

  2. UIWindow 详解及使用场景

    首先来看一下UIWindow 继承关系 方法和属性 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIWindow : UIView //window的屏幕,默认是 [ ...

  3. UIWindow的windowLevel详解

    UIWindow的windowLevel详解

  4. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  5. 详解CALayer 和 UIView的区别和联系

    详解CALayer 和 UIView的区别和联系   前言 前面发了一篇iOS 面试的文章,在说到 UIView 和 CALayer 的区别和联系的时候,被喵神指出没有切中要点,所以这里就 CALay ...

  6. UINavigationController详解一(转)UIBarButtonItem

    本文出自:http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html 特别感谢. 1.UINavigationControlle ...

  7. iOS开发——实用技术OC篇&事件处理详解

    事件处理详解 一:事件处理 事件处理常见属性: 事件类型 @property(nonatomic,readonly) UIEventType     type; @property(nonatomic ...

  8. iOS开发——控制器OC篇&UINavigationController&UITabBarController详解

    UINavigationController&UITabBarController详解 一:UINavigationController 控制器的属性: UINavigationControl ...

  9. iOS中—触摸事件详解及使用

    iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...

随机推荐

  1. 通过OCI 处理 Oracle 10g 中处理Clob大字段写入

    Oracle数据库中, 通过存储过程写入Clob字段 , 当数据大于4k时, 报错 ORA-01460: 转换请求无法实施或不合理 经过排查, 数据Bind方式不对, 不能采用字符串的Bind方式 原 ...

  2. Java中的String,StringBuffer,StringBuilder详解与区别

    1.String Java中string类是不可变的,其中在声明的源代码中用的final,所以只能声明一次.所以每次在明面上的改变其实是重新生成一个String对象,指针指向新的String对象.同时 ...

  3. linux杂记(三)linux指令介绍

    [root@linux ~]# command [-options] parameter1 parameter2 说明: 最左边的root显示的是[目前使用者的账号],而@之后接的是linux即[主机 ...

  4. HTML5 canvas准备知识

    利用canvas来进行画图工作.因此,我们有必要进行一些画图方面的术语说明. 一.画布 在日常生活中,如果我们要画画,可以找纸.板.画布等等工具.而在网页元素中,我们只需要定义一个标签即可. < ...

  5. 重写javascript浮点运算

    javascript中变量存储时不区分number和float类型,同一按照float存储; javascript使用IEEE 754-2008标准定义的64bit浮点格式存储number,decim ...

  6. 50行实现简易HTTP服务器

    话说由于一直很懒,所以博客好像也没怎么更新...今天有空就写一下吧. 最近在看node.js的时候开始对http协议感兴趣了,毕竟node一开始就是为了做web服务器而产生的.于是试着想了一下大概的思 ...

  7. Mocha 从0开始

    Mocha Mocha 是具有丰富特性的 JavaScript 测试框架,可以运行在 Node.js 和浏览器中,使得异步测试更简单更有趣.Mocha 可以持续运行测试,支持灵活又准确的报告,当映射到 ...

  8. Unix/Linux环境C编程入门教程(17) Gentoo LinuxCCPP开发环境搭建

    1. Gentoo Linux是一套通用的.快捷的.完全免费的Linux发行,它面向开发人员和网络职业人员.与其他发行不同的是,Gentoo Linux拥有一套先进的包管理系统叫作Portage.在B ...

  9. 4.跟我学solr---SolrRequestHandler具体解释

    概述 我们在使用solr admin在做查询的时候,能够看到Request-Hander(qt)输入栏中有"/select"这样一个uri.当我们点击查询的时候所发起的请求是这种. ...

  10. Windows内核之进程的终止和子进程

    1 进程终止的方法: <1>主线程的进入点函数返回(最好使用这种方法) <2>进程中的一个线程调用ExitProcesss函数(应该避免使用这样的方法). <3>还 ...