View Controller Programming Guide for iOS
Overview
The Role of View Controllers
UIViewController包含了用来管理view、event、transitioning的所有方法和属性。只需要直接集成该class或者其subclass就可以使用。
两类viewcontroller:
- content view controllers 包含具体内容(包括各种控件)。
- container view controllers 包含其他viewcontroller。
View Management
管理view controller的关键是管理view的层级关系。

一般直接使用
outlet获取view的指针。outlet在view在被从storyboard中读入的时候完成绑定。
content view controller管理内部所有view。而container view controller只是管理root view及其大小、在container中的位置。具体每一个child view内部的内容由每一个对应的child view controller负责管理。

Data Marshaling
view controller负责把具体的数据显示到view。

对于内部的数据结构要独立维护,不要依赖保存在view中的数据。view只适合用来做输入(验证)输出。
User Interactions
view controller不直接处理用户手势动作,而是处理触发的事件。这些事件一般都是通过delegate、action methods方式来执行。
Adaptivity
view controller 负责app在不同设备不同情况(如旋转)下的适应性显示。

The View Controller Hierarchy
维护正确的view层级关系。
The Root View Controller
每一个window都有一个root view controller,是view层级关系的顶点。定义了一些用户可见的内容(如background color)。

获取: UIWindow.rootViewController
Container View Controllers
container view controller作为child view controller的容器使用。方便管理、重用child view controller。一个container view controller可以包含一个以上的child view controller。
container view controller总是填满给它的空间,划分好位置给child view controller。可以嵌套使用。一般作为root view controller(UINavigationController、UISplitViewController、和 UIPageViewController)。

Presented View Controllers
presenting 一个view controller会替换掉当前的view controller的内容,是用来动态显示的常用方式。
- source view controller 调用present方法的view controller,之所以区别与presenting view controller是因为当前view controller不一定符合目标view controller的present条件(如present style)。这个时候UIKit会根据view层次关系树往上找符合条件的view controller并让它成为preseting view controller。有些时候UIKit会让您自己选presenting view controller。
- presetning view controller 指开始present的view controller,不一定是source view controller
- presented view controller 指要被present的view controller
这些需要present的view controller都要先加入到view层次关系中先再present。

区别source view controller和presnting view controller:

Design Tips
Use System-Supplied View Controllers Whenever Possible
Make Each View Controller an Island
Use the Root View Only as a Container for Other Views
Know Where Your Data Lives
Adapt to Changes