如何以编程方式在故事板上设置包含 WKInterfaceController 的初始控制器?
How can I set the initial controller on storyboard contains WKInterfaceController programmatically?
我想在 Watch 应用故事板上有几个视图控制器。问题是我想根据我正在构建的目标加载不同的初始视图控制器。
我可以看到 WKInterfaceController 上有一个名为 "is initial controller" 的值,但我找不到以编程方式设置初始视图的方法。我尝试使用提供的其他方法,但所有方法都将第二个视图推到原始初始视图之上,这不是我想要的(顶部栏上有一个后退按钮)。
如果你有什么办法可以想到支持这个,那就太好了。创建不同的故事板文件是最后的选择。
您实际上要问的是如何控制手表套件应用程序的初始视图控制器。
来自Apple Watch Programming Guide
All interface controllers in a page-based interface are created and
initialized before the interface is displayed, but only one interface
controller at a time is displayed. Normally, WatchKit displays the
first interface controller in the sequence initially. To change the
initially displayed interface controller, call the becomeCurrentPage
method from its init
or awakeWithContext:
method.
如果您使用的是分层界面而不是基于页面的界面,那么您的应用程序将始终以相同的初始视图启动。
其实解决方法很简单。确实,这些页面是在我必须在 运行 时间重建所有页面之前构建的。更多的是标志(是初始控制器)在 运行 时无法更改,但您肯定可以从头开始构建。
我将 WKViewController 的标识符设置为 "exampleViewControllerName"(在 StoryBoard 上)并将此代码添加到 awakeWithContext:context
NSArray *array1=[[NSArray alloc] initWithObjects:@"exampleViewControllerName", nil];
[WKInterfaceController reloadRootControllersWithNames:array1 contexts:nil];
您当然可以使用上下文并使它变得更复杂,但这是它的基础,并且足以提供最简单的答案。
我想在 Watch 应用故事板上有几个视图控制器。问题是我想根据我正在构建的目标加载不同的初始视图控制器。
我可以看到 WKInterfaceController 上有一个名为 "is initial controller" 的值,但我找不到以编程方式设置初始视图的方法。我尝试使用提供的其他方法,但所有方法都将第二个视图推到原始初始视图之上,这不是我想要的(顶部栏上有一个后退按钮)。
如果你有什么办法可以想到支持这个,那就太好了。创建不同的故事板文件是最后的选择。
您实际上要问的是如何控制手表套件应用程序的初始视图控制器。
来自Apple Watch Programming Guide
All interface controllers in a page-based interface are created and initialized before the interface is displayed, but only one interface controller at a time is displayed. Normally, WatchKit displays the first interface controller in the sequence initially. To change the initially displayed interface controller, call the
becomeCurrentPage
method from itsinit
orawakeWithContext:
method.
如果您使用的是分层界面而不是基于页面的界面,那么您的应用程序将始终以相同的初始视图启动。
其实解决方法很简单。确实,这些页面是在我必须在 运行 时间重建所有页面之前构建的。更多的是标志(是初始控制器)在 运行 时无法更改,但您肯定可以从头开始构建。
我将 WKViewController 的标识符设置为 "exampleViewControllerName"(在 StoryBoard 上)并将此代码添加到 awakeWithContext:context
NSArray *array1=[[NSArray alloc] initWithObjects:@"exampleViewControllerName", nil];
[WKInterfaceController reloadRootControllersWithNames:array1 contexts:nil];
您当然可以使用上下文并使它变得更复杂,但这是它的基础,并且足以提供最简单的答案。