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