启动循环中基于页面的“reloadRootControllersWithNames:”?
- (void)awakeWithContext:(id)context { [super awakeWithContext:context]; [WKInterfaceController reloadRootControllersWithNames:@[@"pageOne", @"pageTwo"] contexts:nil]; }
遵循Apple的指导方针
调用此方法可在应用程序基于页面的界面中重新加载页面。 在启动时,您可以使用此方法自定义要显示的页面集。
在发布时,只会导致循环。 每次重新加载调用awakeWithContext或将一次又一次地激活或初始化。
有没有更好的方法可以在启动循环时重新加载基于页面的应用程序?
这是WatchKit应用程序的常见问题,因为我们不再需要UIApplicationDelegate
来处理此类设置。 一个好方法是按如下方式构造代码:
-
MainInterfaceController
(故事板中的主要链接指向此处) -
PageOneInterfaceController
– 您在页面集中显示的第一个界面 -
PageTwoInterfaceController
– 页面集中的第二个接口
MainInterfaceController
永远不会实际显示。 您将始终启动到另一组接口控制器,具体取决于MainInterfaceController.awakeWithContent()
随附iOS应用程序的缓存状态。 这样,您使用MainInterfaceController
的方式类似于我们在iOS中使用UIApplicationDelegate
来设置窗口和根视图控制器。
我在一个应用程序中使用了这种方法,该应用程序有许多不同的页面集可供选择,并且它运行良好。
这就是为什么awakeWithContext:
存在。 第一次启动应用程序时,初始控制器作为context
传递为nil
。 但是如果你reloadRootControllersWithNames:contexts:
,你有机会传递自定义上下文实例,从而区分启动模式。
真的很容易解决,并且不需要多页控制器 – 只需使用一次
创建一个类变量(不是实例变量)并将其用作您的标志,以确保只调用一次reloadRootControllers调用。
static NSString* hasLaunchedIfNotNullString = NULL; - (void)awakeWithContext:(id)context { if(hasLaunchedIfNotNullString == NULL) { //START Code which gets executed once hasLaunchedIfNotNullString = @""; ... [WKInterfaceController reloadRootControllersWithNames:YOUR_ARRAY contexts:CONTEXTS]; // END code which gets executed once } }