WatchOS系统开发大全(5)-WKInterfaceController的生命周期
1.1-簡(jiǎn)介
-
WKInterfaceController是我們開(kāi)發(fā)Watch App的核心類(lèi),它的地位和之前使用的UIViewController一樣。
-
每一個(gè)Watch App構(gòu)建時(shí),至少需要在Storyboard上設(shè)置一個(gè)WKInterfaceController實(shí)例作為程序入口。我們可以在Storyboard上使用Main Entry Point設(shè)置。
-
當(dāng)用戶(hù)launch了Watch App時(shí),Watch OS 會(huì)開(kāi)始加載程序中的Storyboard。我們?cè)赟toryboard中為每一個(gè)WKInterfaceController設(shè)置的響應(yīng)事件,會(huì)在用戶(hù)觸發(fā)時(shí)在WatchKit Extension中響應(yīng)。我們可以像以前一樣push, pop, present 目標(biāo)WKInterfaceController。
1.2-生命周期
0601.png
1.3-框架
-
(1)當(dāng)Watch OS加載App中的Storyboard時(shí),iPhone端也會(huì)開(kāi)始加載對(duì)應(yīng)的WatchKit Extension。
-
(2)當(dāng)Watch OS開(kāi)始初始化我們Watch App的Storyboard中的UI時(shí),iPhone端WatchKit Extension會(huì)生成對(duì)應(yīng)的WKInterfaceController,并且響應(yīng)initWithContext:方法。
-
(3)當(dāng)Watch OS顯示當(dāng)前加載的UI時(shí),WatchKit Extension中對(duì)應(yīng)的WKInterfaceController響應(yīng)willActivate方法。
-
(4)當(dāng)用戶(hù)切換頁(yè)面或者停止使用時(shí),WatchKit Extension中對(duì)應(yīng)的WKInterfaceController響應(yīng)didDeactivate方法。
0603.png
1.4-使用
-
從上圖可知這三個(gè)API,對(duì)應(yīng)了Watch OS加載一個(gè)視圖控制器的三個(gè)狀態(tài)。我們?cè)谧约旱腤KInterfaceController類(lèi)中,應(yīng)該實(shí)現(xiàn)這三個(gè)API用來(lái)處理不同的情況:
- initWithContext: 我們可以在這里加載數(shù)據(jù)或者更新在StoryBoard中當(dāng)前Controller添加的interface objects。
- willActivate 我們可以在這里更新interface objects或者處理其他事件
- didDeactivate 我們應(yīng)該在這里清理task或者數(shù)據(jù)。在這里更新interface objects將會(huì)被系統(tǒng)忽略。
總結(jié)
以上是生活随笔為你收集整理的WatchOS系统开发大全(5)-WKInterfaceController的生命周期的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: WatchOS系统开发大全(4)-Wat
- 下一篇: WatchOS系统开发大全(6)-WKI