【iOS】UIViewControllerのライフサイクル (Swift)
いまさら聞けない、
Swift UIViewControllerのライフサイクル
下記が主なイベント
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("viewWillAppear")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("viewDidAppear")
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
print("viewWillLayoutSubviews")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("viewDidLayoutSubviews")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("viewWillDisappear")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("viewDidDisappear")
}
}
下記のような感じに呼ばれます。
viewDidLoad
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
-- 表示完了
viewWillDisappear
viewDidDisappear
これくらい、余裕ですよね?
追記 prettyprint を追加したのでみやすいように下記追加しました。
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() print("viewDidLoad") } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) print("viewWillAppear") } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) print("viewDidAppear") } override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() print("viewWillLayoutSubviews") } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() print("viewDidLayoutSubviews") } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) print("viewWillDisappear") } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) print("viewDidDisappear") } }下記のような順番でよばれます。
開始時
viewDidLoadviewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
終了時
viewWillLayoutSubviewsviewDidLayoutSubviews
viewWillDisappear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidDisappear
#ライフサイクル
#Lifecycle
#UIViewController
#Swift
コメント
コメントを投稿