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