如何在 WatchKit PageController 中设置起始页

How to set starting page in WatchKit PageController

我有一个页面控制器 WatchKit 应用程序,我要在其中插入一个新页面。我想将显示的默认页面设置为刷新前我所在的索引,但我不知道如何设置首先选择哪个页面(它始终默认为索引 0 处的页面)。以下是我刷新它们的方式:

for (int i = 0; i < self.models.count; i++)
{
    [names addObject:@"MyInterfaceController"];
    [objects addObject:self.models[i]];
}

[WKInterfaceController reloadRootControllersWithNames:names contexts:objects];

有谁知道如何以编程方式设置所选页面?

初始顺序由您调用reloadRootControllers时您的namescontexts的顺序决定。但是,您可以使用 WKInterfaceController 上的 becomeCurrentPage 方法来更改当前页面。

在此处查看文档:https://developer.apple.com/library/prerelease/ios/documentation/WatchKit/Reference/WKInterfaceController_class/#//apple_ref/occ/instm/WKInterfaceController/becomeCurrentPage

恐怕目前无法做到这一点。充其量,如 bgilham 所述,您可以让您的应用程序启动到第 0 页,然后动画显示到另一页。但这听起来根本不像您要寻找的那种行为。

watchOS 4 现在有了满足您需要的方法。查看:https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/2868441-reloadrootpagecontrollers

WKInterfaceController.reloadRootPageControllers(withNames: names,
                                                contexts: contexts, 
                                                orientation: .horizontal, 
                                                pageIndex: startPageIndex)

您可能已经猜到了,最后一个参数是起始页的索引。没有动画的作品,就是你想要的:)