Life cycle of View 生命週期
loadView:用程式碼產生畫面
viewDidLoad:讀入畫面之後執行
第一個載入的方法,可以按照自己希望App一開始呈現的畫面來設定。
只有在一開始執行App時呼叫一次。
viewWillAppear:畫面即將顯示到螢幕上
viewDidAppear:畫面已經顯示到螢幕上
當畫面呈現給使用者時,就呼叫此方法,每次進入畫面時都會呼叫。
viewWillDisappear:畫面即將離開螢幕
veiwDidDisappear:畫面已經離開螢幕
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
print("view: did load")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("view: will appear")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("view: did appear")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("view: will disapper")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidAppear(animated)
print("view: did disappear")
}
}
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool{
print("app: did finishing launching")
return true
}
func applicationWillResignActive(_ application: UIApplication){
print("app: will resign active")
}
func applicationDidEnterBackground(_ application: UIApplication){
print("app: did enter background")
}
func applicationWillEnterForeground(_ application: UIApplication){
print("app: did enter foreground")
}
func applicationDidBecomeActive(_ application: UIApplication){
print("app: did become active")
}
func applicationWillTerminate(_ application: UIApplication){
print("app: will terminate")
}