WatchOS系统开发大全(5)-WKInterfaceController的生命周期

来源:互联网 发布:真正刷钻软件 编辑:程序博客网 时间:2024/06/05 14:34

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-生命周期


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加载一个视图控制器的三个状态。我们在自己的WKInterfaceController类中,应该实现这三个API用来处理不同的情况:

    • initWithContext: 我们可以在这里加载数据或者更新在StoryBoard中当前Controller添加的interface objects。
    • willActivate 我们可以在这里更新interface objects或者处理其他事件
    • didDeactivate 我们应该在这里清理task或者数据。在这里更新interface objects将会被系统忽略。
阅读全文
0 0
原创粉丝点击