1.1-簡介
WKInterfaceController是我們開發Watch App的核心類,它的地位和之前使用的UIViewController一樣。
每一個Watch App構建時,至少需要在Storyboard上設置一個WKInterfaceController實例作為程序入口。我們可以在Storyboard上使用Main Entry Point設置。
當用戶launch了Watch App時,Watch OS 會開始加載程序中的Storyboard。我們在Storyboard中為每一個WKInterfaceController設置的響應事件,會在用戶觸發時在WatchKit Extension中響應。我們可以像以前一樣push, pop, present 目標WKInterfaceController。
1.2-生命周期
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方法。
1.4-使用
-
從上圖可知這三個API,對應了Watch OS加載一個視圖控制器的三個狀態。我們在自己的WKInterfaceController類中,應該實現這三個API用來處理不同的情況:
- initWithContext: 我們可以在這里加載數據或者更新在StoryBoard中當前Controller添加的interface objects。
- willActivate 我們可以在這里更新interface objects或者處理其他事件
- didDeactivate 我們應該在這里清理task或者數據。在這里更新interface objects將會被系統忽略。