macOS學(xué)習(xí)之OS X View Controllers


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

  1. 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.
  2. 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.
  3. viewDidAppear() is called after the view appears on screen. Here you can start some fancy animations.

翻譯:


  1. viewDidLoad() 視圖被完全載入的時(shí)候只會(huì)被調(diào)用一次,并且能夠被用于一次性的初始化庸推,比如number formatter的配置,注冊(cè)通知予弧,或者API的調(diào)用這些只需做一次的操作
  2. viewWillAppear() 每次視圖顯示在屏幕中的時(shí)候都會(huì)被調(diào)用。在我們的應(yīng)用中掖蛤,當(dāng)你每次選擇Overview tab的時(shí)候都會(huì)調(diào)用它。這是更新UI或刷新數(shù)據(jù)的好方法蚓庭。
  3. 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è)方法具體到這一階段:

  1. updateViewConstraints() is called every time the layout changes, like when the window is resized.
  2. viewWillLayout() is called before the layout() method of a view controller’s view is called. For example, you can use this method to adjust constraints.
  3. viewDidLayout() is called after layout() is called.

翻譯:


  1. updateViewConstraints() 每次layout改變都會(huì)調(diào)用這個(gè)方法。如窗口大小的變化夏醉。
  2. viewWillLayout() 會(huì)在前一個(gè)視圖控制器視圖的layout() 方法調(diào)用前調(diào)用,例如畔柔,你可以用這個(gè)方法來調(diào)整約束條件
  3. 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)的方法玄捕。

  1. viewWillDisappear() is called before the view disappears. Here you can stop your fancy animations you started in viewDidAppear().
  2. 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.

翻譯:


  1. viewWillDisappear() 會(huì)在視圖即將消失的時(shí)候調(diào)用。在這里你可以停止你在viewDidAppear() 中開始的動(dòng)畫
  2. 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() and viewDidAppear(). The best solution is to use viewWillAppear() so that the user sees the updated UI at the moment the view appears. Using viewDidAppear() 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ì)被顯示在視圖上腿宰。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市甩挫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌椿每,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,946評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件删壮,死亡現(xiàn)場(chǎng)離奇詭異贪绘,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)税灌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來菱涤,“玉大人,你說我怎么就攤上這事粘秆。” “怎么了?”我有些...
    開封第一講書人閱讀 169,716評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵此再,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我玲销,道長(zhǎng),這世上最難降的妖魔是什么贤斜? 我笑而不...
    開封第一講書人閱讀 60,222評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮瘩绒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘锁荔。我一直安慰自己,他們只是感情好堕战,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著嘱丢,像睡著了一般。 火紅的嫁衣襯著肌膚如雪越驻。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,807評(píng)論 1 314
  • 那天缀旁,我揣著相機(jī)與錄音,去河邊找鬼并巍。 笑死目木,一個(gè)胖子當(dāng)著我的面吹牛刽射,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播剃执,決...
    沈念sama閱讀 41,235評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼肾档!你這毒婦竟也來了辫继?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,189評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤骇两,失蹤者是張志新(化名)和其女友劉穎姜盈,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體馏颂,經(jīng)...
    沈念sama閱讀 46,712評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評(píng)論 3 343
  • 正文 我和宋清朗相戀三年救拉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片亿絮。...
    茶點(diǎn)故事閱讀 40,926評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖派昧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蒂萎,我是刑警寧澤秆吵,帶...
    沈念sama閱讀 36,580評(píng)論 5 351
  • 正文 年R本政府宣布纳寂,位于F島的核電站,受9級(jí)特大地震影響泻拦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜争拐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望陆错。 院中可真熱鬧,春花似錦音瓷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杏愤。三九已至,卻和暖如春珊楼,著一層夾襖步出監(jiān)牢的瞬間通殃,已是汗流浹背厕宗。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留已慢,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,368評(píng)論 3 379
  • 正文 我出身青樓佑惠,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親膜楷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子旭咽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,592評(píng)論 0 23
  • /* UIViewController is a generic controller base class th...
    DanDanC閱讀 1,823評(píng)論 0 2
  • 下午約朋友去吃飯轻专,因?yàn)闀r(shí)間還早察蹲,我們打算走著去,反正也不太遠(yuǎn)洽议。 路過一個(gè)公交站宗收,有個(gè)中年男子站在公交站背后的人行道...
    沐亦閱讀 344評(píng)論 2 1
  • 說實(shí)話讀《圍城》是2010年的7月到現(xiàn)在已經(jīng)6年了混稽,再?zèng)]拿起來讀過。腦海中故事里的人物早已模糊审胚,只是依稀記得一些情...
    帶著面具的陌生人閱讀 357評(píng)論 0 1
  • 文/逸溪 長(zhǎng)夜未央,獨(dú)自彷徨膳叨。 月色蒼茫,情話寸腸菲嘴。 踏歌亭上饿自,戀人淚殤。 思念無常昭雌,竟赴凄涼复唤。 輕步空廊烛卧,留字千...
    逸溪閱讀 539評(píng)論 0 1