對於 View Controller 的各個執行週期,很多前輩都有詳細的介紹。
不過我想實作一次,還是最能夠記住的,以下是我的實作方法分享。
- 開啟 storyboard,從 Object library 拉進來另一個 View Controller。
- file -> new -> file 加入一個新的 Cocoa Touch Class ,將 Class 命名為SecondViewController。
- 到 storyboard 將步驟1新增的 ViewController 與步驟2加入的 Class 做出連結。
- 到 storyboard 中,在 Second View Controller 中加入一個 UIView 設定為紅色,主要是比較容易看出轉換。
- 到 storyboard 中,在 Second View Controller 中加入一個 UIButton ,裡面文字改為back。
- 在原始的 View Controller 中加入一個 UIButton ,裡面文字改為 go,按住 control 鍵,點選該 button 連到 Second View Controller 中建立 segue。
- 點選 segue ,到 Attributes inspector 中,設定 identifier 為 goToSecondViewController。
以上,都還沒輸入到程式碼。
接下來進行程式碼的部分,因為我希望可以知道每一個階段執行的時間點,所以我在兩個 View Controller 的程式碼部分,除了原本的 ViewDidLoad 之外,都另外加入了 viewWillAppear 、viewDidAppear 、 viewWillDisappear 、 viewDidDisappear ,並且在個階段都用 print 來顯示執行的順序。
ViewController.swift 裡面,輸入程式碼如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
@IBAction func unwind(segue: UIStoryboardSegue){ | |
if segue.identifier == "goToSeconViewController" { | |
segue.source as! SecondViewController | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
print("firstViewDidLoad") | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
print("firstViewWillAppear") | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
print("firstViewDidAppear") | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
print("firstviewWillDisappear") | |
} | |
override func viewDidDisappear(_ animated: Bool) { | |
print("firstviewDidDisappear") | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
SecondViewController.swift 裡面,輸入程式碼如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class SecondViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
print("2ViewDidLoad") | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
print("2ViewWillAppear") | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
print("2ViewDidAppear") | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
print("2viewWillDisappear") | |
} | |
override func viewDidDisappear(_ animated: Bool) { | |
print("2viewDidDisappear") | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
最後一步,在 storyboard 中, 點選 Second View Controller 中的 back 按鈕,按住 control 鍵 拉到上方的 exit 之後,會跳出 unwindWithSeague 的選項(來自我們在 ViewController.swift 中第5行輸入的 func ),點選它,這樣子 back 按鈕才會起作用,返回 View Controller。
接下來就可以按下編譯與執行鍵。
這邊示範的是,開啟第一個畫面之後,按 go 跳到第二個畫面,再按 back 回到第一畫面,print 出來的結果順序如下:
firstViewDidLoad
firstViewWillAppear
firstViewDidAppear
2ViewDidLoad
firstviewWillDisappear
2ViewWillAppear
2ViewDidAppear
firstviewDidDisappear
2viewWillDisappear
firstViewWillAppear
firstViewDidAppear
2viewDidDisappear
可以看到,ViewDidLoad 只有打開 app 的時候會執行一次,換到別的 scene 再回到第一頁之後,就不會再執行 ViewDidLoad,而 ViewWillAppear 及 ViewDidAppear 則是每次轉換 scene 時都會再執行一次。
後來又寫了一篇,如果按下 home 鍵會發生什麼狀況呢?
請看:iOS Develop – app View Lifecycle測試 (2)
大家有興趣可以自己去體驗一下,提供完整project如下:
https://www.dropbox.com/sh/5eqm7jj3g7uwuwt/AADcjEA7rdgiKS0CcIWXCGzca?dl=0
參考資料:
iOS Develop – app View Lifecycle測試 (1) 有 “ 1 則迴響 ”