相信大家在初初開始進行iOS編程時,都曾被各種 viewDidLoad
、 viewWillAppear
、 viewDidAppear
等各種各樣的事件Func弄得頭暈眼花,所以今天「暢想資源」就來舉幾個例子來讓大家可以對這些ViewController上不同的事件更爲瞭解!(附Swift程式碼)
影片預覽
viewDidLoad
View 被載入時:
1 2 3 4 5 6 |
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. println("viewDidLoad") } |
viewWillAppear
View 要被呈現前,發生於 viewDidLoad 之後:
1 2 3 4 5 |
override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) println("viewWillAppear") } |
viewDidAppear
View 呈現後,發生於 viewWillAppear 之後:
1 2 3 4 5 |
override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) println("viewDidAppear") } |
viewWillDisappear
View 要結束前,要切換到下一個 View 時會發生此事件:
1 2 3 4 5 |
override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) println("viewWillDisappear") } |
viewDidDisappear
View 完全結束後,發生於 viewWillDisappear 之後:
1 2 3 4 5 |
override func viewDidDisappear(animated: Bool) { super.viewDidDisappear(animated) println("viewDidDisappear") } |