View Controller

来源:互联网 发布:微软授权淘宝经销商 编辑:程序博客网 时间:2024/05/19 11:19

如果想要展示一个View Controller,一般有如下一种途径

  1. 设置成UIWindow的rootViewController(iOS 4.0之前UIWindow并没有rootViewController属性,只能通过addSubview的方式添加一个View Controller的view)
  2. 使用某个已经存在的Container来展示,比如使用UINavigationController来展示某个View Controller [navigationController pushViewController:vc animated:YES];
  3. 以模态界面的方式展现出来 presentModalViewController
  4. 以addSubview的方式将使其view作为另一个View Controller的view的subView

直接使用4种方法是比较危险的,上一级 View Controller并不能对当前View Controller的 生命周期相关的函数进行调用,以及旋转事件的传递等。

我们常用的容器有 UINavigationController,UITabbarController等,一般容器有一些共同的特征:

  1. 提供对Child View Controller进行管理的接口,比如添加Child View Controller,切换Child View Controller的显示,移除Child View Controller 等
  2. 容器“拥有”所有的Child View Controller
  3. 容器需要负责 Child View Controller的appearance callback的调用(viewWillAppear,viewDidAppear,viewWillDisaapper,viewDidDisappear),以及旋转事件的传递
  4. 保证view hierarchy 和 view controller hierarchy 层级关系一致,通过parent view controller将child view controller和容器进行关联

从上面可以看出来,实现一个Container View Controller并不是一个简单的事情,好在iPhone的界面大小有限,一般情况下一个View Controller的view都是充满界面或者系统自带容器的,我们无需自己创建额外的容器。


0 0
原创粉丝点击