View Controller Life Cycle
Since view controllers are responsible for managing views, they expose methods that allow you to hook into events associated with the views. For example the point at which the views have loaded from the storyboard, or when the views are about to appear on the screen. This collection of event-based methods are known as the view controller life cycle.
The life cycle of a view controller can be divided into three major parts: its creation, its lifetime, and finally its termination. Each part has methods you can override to do additional work.
翻譯:
由于視圖控制器負(fù)責(zé)管理視圖田轧,所以它們會(huì)暴露允許您與視圖關(guān)聯(lián)的事件的方法.鞍恢。例如,在這一點(diǎn)上視圖已經(jīng)從storyboard中加載帮掉,或者視圖將出現(xiàn)在屏幕上的時(shí)候。這種基于事件的方法的集合被稱為視圖控制器生命周期蟆炊。
一個(gè)視圖控制器的生命周期可以分為三個(gè)主要部分:它的創(chuàng)建,它的生命時(shí)間涩搓,并最終終止劈猪。每個(gè)部分都有方法可以重寫做額外的工作.良拼。
Creation
-
viewDidLoad()
is called once the view is fully loaded and can be used to do one-time initializations like the configuration of a number formatter, register for notifications, or API calls that only need to be done once. -
viewWillAppear()
is called every time the view is about to appear on screen. In our application, it is called every time you select the Overview tab. This is a good point to update your UI or to refresh your data model. -
viewDidAppear()
is called after the view appears on screen. Here you can start some fancy animations.
翻譯:
-
viewDidLoad()
視圖被完全載入的時(shí)候只會(huì)被調(diào)用一次,并且能夠被用于一次性的初始化庸推,比如number formatter的配置,注冊(cè)通知予弧,或者API的調(diào)用這些只需做一次的操作 -
viewWillAppear()
每次視圖顯示在屏幕中的時(shí)候都會(huì)被調(diào)用。在我們的應(yīng)用中掖蛤,當(dāng)你每次選擇Overview tab的時(shí)候都會(huì)調(diào)用它。這是更新UI或刷新數(shù)據(jù)的好方法蚓庭。 -
viewDidAppear()
當(dāng)視圖已經(jīng)顯示到屏幕上的時(shí)候會(huì)被調(diào)用,這里你可以開始一些花式的動(dòng)畫器赞。
Lifetime
Once a view controller has been created, it then enters a period during which it it handles user interactions. It has three methods specific to this phase of its life:
翻譯:
一旦創(chuàng)建了一個(gè)視圖控制器,它就進(jìn)入一個(gè)處理用戶交互的周期港柜。它有三個(gè)方法具體到這一階段:
-
updateViewConstraints()
is called every time the layout changes, like when the window is resized. -
viewWillLayout()
is called before thelayout()
method of a view controller’s view is called. For example, you can use this method to adjust constraints. -
viewDidLayout()
is called afterlayout()
is called.
翻譯:
-
updateViewConstraints()
每次layout改變都會(huì)調(diào)用這個(gè)方法。如窗口大小的變化夏醉。 -
viewWillLayout()
會(huì)在前一個(gè)視圖控制器視圖的layout()
方法調(diào)用前調(diào)用,例如畔柔,你可以用這個(gè)方法來調(diào)整約束條件 -
viewDidLayout()
在layout()
調(diào)用方法之后調(diào)用。
In all three methods, you must call the super implementation at some point.
翻譯:
在這三個(gè)方法中靶擦,你必須要調(diào)用他們的父類實(shí)現(xiàn)。
Termination
These are the counterpart methods to creation:
翻譯:
這些是和創(chuàng)建方法相對(duì)應(yīng)的方法玄捕。
-
viewWillDisappear()
is called before the view disappears. Here you can stop your fancy animations you started inviewDidAppear()
. -
viewDidDisappear()
is called after the view is no longer on the screen. Here you can discard everything you no longer need. For example, you could invalidate a timer you used to update your data model on a periodic time base.
翻譯:
-
viewWillDisappear()
會(huì)在視圖即將消失的時(shí)候調(diào)用。在這里你可以停止你在viewDidAppear()
中開始的動(dòng)畫 -
viewDidDisappear ()
當(dāng)視圖從屏幕上消失的時(shí)候會(huì)調(diào)用這個(gè)方法桩盲,在這里你可以丟棄你不再需要的一切。例如,你可以暫停你的定時(shí)器去更新你的數(shù)據(jù)模型孝冒。
Life cycle in practice
Now that you know the most important things about a view controller’s life cycle, it’s time for a short test!
Question: Every time OverviewController’s view appears, you want to update the UI to take into account that a user selected a product when the Details tab was selected. Which method would be a good fit?
翻譯:
現(xiàn)在你知道了一個(gè)視圖控制器生命周期中最重要的事情,現(xiàn)在是進(jìn)行短測(cè)試的時(shí)候了庄涡!
問題:每次 OverviewController’s 的視圖出現(xiàn),當(dāng)用戶選擇Details Tab的時(shí)候穴店,你想去更新用戶的UI。哪個(gè)方法是最佳的選擇拿穴?
Solution Inside
There are two possible methods:viewWillAppear()
andviewDidAppear()
. The best solution is to useviewWillAppear()
so that the user sees the updated UI at the moment the view appears. UsingviewDidAppear()
means that a user would see the UI appear first showing old data before updating.
翻譯:
解決方案
有兩種可能的方法:viewwillappear()
和viewdidappear()
。最好的解決辦法是使用viewwillappear()
讓用戶看到更新的用戶界是在視圖將要顯示的時(shí)候球凰。使用viewdidappear()
意味著用戶將要看到第一次顯示UI的時(shí)候,舊的數(shù)據(jù)將會(huì)被顯示在視圖上腿宰。