ViewController是iOS應用程序中重要的部分,是應用程序數據和視圖之間的重要橋樑。且應用程序至少有一個view controller。每一個view controller對象都負責和管理一個view對象,稱此對象爲root view,其組織和管理以root view爲根的視圖層次結構。 app
圖 11 佈局
能夠將view controller分爲兩種類型: spa
1) Content:用於管理視圖中的分散view控件,這些分散view控件是app中的主要實體。 3d
2) Container:做爲一種容器,用於存放和管理其它view controller,從而實現導航和表示。 對象
1 Root View Controller blog
Root view controller(簡寫爲RVC)是全部 view controller的支柱,而且window使用RVC來填充其內容視圖,即RVC做爲window的子視圖,app將全部可視化組件都放在了RVC之中,或者是RVC子視圖中,如圖 12所示。 it
圖 12 The root view controller io
在UIWindow對象中有一個rootViewController屬性,其描述root VC對象。若是經過storyboards配置VC,那麼UIKit會自動設置UIWindow的rootViewController屬性;若經過程序建立window,那麼須要手動設置window的rootViewController屬性。 容器
2 Container View Controllers 可視化
Container view controllers(簡寫爲CVC)能夠將離散的視圖或組件裝配成複雜的app,從而可以複用這些組件或視圖。UIKit定義了一些CVC容器,有UINavigationController、 UISplitViewController和UIPageViewController。
CVC負責管理子view controller的空間佈局和位置,而且常常做爲Root view controller,若圖 13所示。固然CVC能夠做爲任何 view controller的父容器,包括其它的CVC。
圖 13 A container acting as the root view controller
3 Presented View Controllers
Presented View Controllers(簡寫爲PVC)常常用於替換當前顯示的view controller,即將PVC做爲新顯示的內容。當present一個新的view controller做爲顯示的內容,那麼UIKit將會爲presenting view controller 和presented view controller之間建立一種關係,如圖 14所示。
圖 14 Presented view controllers
當present一個新的view controller時,UIKit會尋找一個能提供合適上下文的Container view controllers,大多數狀況下是選擇離最近的Container view controllers;有時須要用戶手動指定Container view controllers做爲此次present提供的上下文。
如圖 15所示,描述了一個涉及Container view controllers的present操做,當屏幕顯示黃色了是view時,present了綠色的視圖。這時不是將黃色view所關聯的controller做爲presenting view controller,而是將navigation controller做爲presenting view controller。
圖 15 A container and a presented view controller