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

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

填寫項(xiàng)目名,Language選Swift,表示使用Swift語言。

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

點(diǎn)擊
New File

選擇
Cocoa Touch Class

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

因?yàn)樯厦嫖覀冋f要加一個(gè)帶有導(dǎo)航條的
ViewController。所以這里設(shè)置的是UINavigatioController。
修改新的視圖控制器
我們來寫一個(gè)例子,在RootViewController中添加一個(gè)按鈕,點(diǎn)擊按鈕跳入另一個(gè)視圖控制器中,然后在另一個(gè)視圖控制器中添加一個(gè)按鈕,點(diǎn)擊退回到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)方式 成對(duì)出現(xiàn)
dismiss(animated: true, completion: nil)
}
}

