更新:2018.05.24
整理了一下demo:SwiftDemo
在iOS應用運行中,屏幕中顯示的內(nèi)容是一組一組的視圖對象,他們負責顯示屏幕中的內(nèi)容,而在視圖的后面是UIViewController視圖控制器,它的作用是管理哪些視圖中顯示的內(nèi)容,并協(xié)調(diào)他們和應用其他部分的關系。
UIViewController的主要作用:
- 在一個復雜的iOS應用中, 往往包含多個屏幕的內(nèi)容,使用UIViewController可以方便管理眾多的內(nèi)容。
- UIViewController類負責創(chuàng)建其管理的視圖,并在內(nèi)存較低的時候講他們從內(nèi)容中移除。
- 可以通過視圖控制器來將新的視圖內(nèi)容,以模態(tài)窗口的方式顯示在當前視圖的上方。
- 視圖控制器可以相應設備的方向變化,對視圖進行相應的調(diào)整,以適應屏幕的新方向。
- 一些特殊的視圖控制器,如導航視圖控制器、標簽視圖控制器,可使視圖的管理更加方便和規(guī)范。
視圖控制器的生命周期:
-
alloc
創(chuàng)建一個視圖控制器對象,并分配內(nèi)存空間。 -
init()
對視圖控制器對象進行初始化。 -
loadView
如果從storyboard創(chuàng)建視圖,則從storyboard中加載視圖。 -
viewDidLoad
視圖加入完成,可以進行一些自定義操作 -
viewWillAppear
視圖即將要展示在屏幕上。 -
viewDidAppear
視圖已經(jīng)站在屏幕上顯示并完成渲染。 -
viewWillLayoutSubviews
視圖即將布局其子視圖 -
viewDidLayoutSubviews
視圖已經(jīng)完成子視圖的布局 -
viewWillDisappear
視圖即將從屏幕中消失 -
viewDidDisappear
視圖已經(jīng)從屏幕上消失 -
dealloc
視圖被銷毀
創(chuàng)建一個UIViewController
相信大家都知道MVC,在MVC中,UIViewController扮演者控制層(C)的角色,UIViewController的職責是:對內(nèi)管理與之關聯(lián)的UIView,對外跟其他UIViewController通信和協(xié)調(diào)。
創(chuàng)建UIViewController之前,我們先新建一個項目,新建項目后,可以使用代碼活通過StoryBoard創(chuàng)建一個UIViewController。

一般情況下,我們選擇
Single View Application來創(chuàng)建一個新項目

填寫項目名,Language選Swift,表示使用Swift語言。

可以看到新項目里已經(jīng)有一個ViewController項目,而不同于OC的是,沒有了.h 和.m文件,只有一個.swift后綴的文件。
一般在我們正常開發(fā)過程中,新建項目的這個ViewController很難滿足我們的要求,比如我們的rootViewController需要的是一個有導航條的ViewController,那么我們就要自己創(chuàng)建一個。

點擊
New File

選擇
Cocoa Touch Class

輸入一個類名,并選
UIViewController
OK,我們創(chuàng)建了一個新的UIViewController:RootViewControlelr,現(xiàn)在我們來修改初始視圖控制器:使用代碼修改初始視圖控制器在Swift中要比OC中簡單的多,只需要在AppDelegat中的didFinishLaunchingWithOptions方法中設置window.rootViewController,

因為上面我們說要加一個帶有導航條的
ViewController。所以這里設置的是UINavigatioController。
修改新的視圖控制器
我們來寫一個例子,在RootViewController中添加一個按鈕,點擊按鈕跳入另一個視圖控制器中,然后在另一個視圖控制器中添加一個按鈕,點擊退回到RootViewController中。
rootViewController中:
import UIKit
class RootViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.green
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 50)
button.backgroundColor = UIColor.white
button.setTitle("打開新的視圖控制器", for: .normal)
button.setTitleColor(UIColor.gray, for: .normal)
view.addSubview(button)
button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
}
func buttonClick() {
navigationController?.pushViewController(ViewController(), animated: true)
// 另一種跳轉(zhuǎn)方式
// present(ViewController(), animated: true, completion: nil)
}
}
ViewController中
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.red
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 50)
button.backgroundColor = UIColor.white
button.setTitle("回到RootViewController", for: .normal)
button.setTitleColor(UIColor.gray, for: .normal)
view.addSubview(button)
button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
}
func buttonClick() {
navigationController?.popViewController(animated: true)
// 另一種跳轉(zhuǎn)方式 成對出現(xiàn)
dismiss(animated: true, completion: nil)
}
}

