这篇文章向大家展示如何使用WatchKit创建Page-Based Navigation,我们还是在Helloworld项目的基础上进行改善。
创建Controller
打开Interface.storyboard,再添加两个Interface Controller,然后给它们添加不同的背景色。

现在给每个Controller之间添加Segue,选中第一个Controller,按着control+鼠标左键拖拽至第二个Controller,然后选择next page。

编译运行,看看它是如何工作的:

Interface Controller生命周期
接下来我们自定义两个WKInterfaceController,分别设置给新添加的两个Interface Controller。然后我们来看看InterfaceController的生命周期。首先在GreenInterfaceController和RedInterfaceController中添加如下三个方法:

编译运行,当Watch模拟器显示出内容时,在控制台中我们可以看到GreenInterfaceController和RedInterfaceController执行了init方法:

当切换到绿色InterfaceController时,在控制台中可以看到GreenInterfaceController执行了willActivate方法:

当切换到粉红色InterfaceController时,在控制台中可以看到RedInterfaceController执行了willActivate方法,而GreenInterfaceController执行了didDeactivate方法:

所以由此可见InterfaceController的生命周期主要由三个方法组成:
- initWithContext:当Watch App第一次启动,把三个InterfaceController压入栈的时候调用该方法。
- willActivate:当进入InterfaceController时,调用该方法。类似
UIViewController中的viewDidAppear方法。 - didDeactivate:当InterfaceController不在当前屏幕显示时调用该方法。类似
UIViewController中的viewDidDisappear方法。