如何让Apple Watch更新用WKInterfaceController创建的页面reloadRootControllersWithNames:contexts?

时间:2015-06-17 01:12:23

标签: ios watchkit apple-watch watch

我有一个用于watchkit应用程序的WKInterfaceController,InterfaceController.m。它创建了这样的分页内容:

- (void)willActivate {
    [super willActivate];
    NSDate *now = [[NSDate alloc] init];
    NSDictionary *time = @{@"time":[NSString stringWithFormat:@"%f",[now timeIntervalSince1970]]};
    NSArray *contexts = [[NSArray alloc] initWithObjects:time,time,nil];
    NSArray *names = [[NSArray alloc] initWithObjects:@"page",@"page",nil];
    [WKInterfaceController reloadRootControllersWithNames:names contexts:contexts];
}

带有"页面&#34的WKInterfaceController;因为标识符被称为PageInterfaceController.m并且只显示InterfaceController.m发送的时间

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    [label setText:[context objectForKey:@"time"]];
}

第一次加载时效果很好,但在退出并重新启动应用程序后,它会显示旧时代。如何让应用程序返回InterfaceController.m并更新时间?

是的,我知道我可以把时间放在PageInterfaceController中,但在我的真实应用程序中,InterfaceController获取位置数据并将其发送到页面,甚至确定要创建的页面数。

您可以在此处下载整个示例项目:

https://github.com/tomkincaid/WatchInterfaceTest

2 个答案:

答案 0 :(得分:0)

解决方法是使用计时器并在一段时间后返回第一个接口控制器。不理想,因为我希望每次启动手表应用程序时都可以从第一个界面控制器启动它。

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    time = [context objectForKey:@"time"];
}

- (void)willActivate {
    [super willActivate];
    NSDate *now = [[NSDate alloc] init];
    double diff = [now timeIntervalSince1970] - [time doubleValue];
    if (diff > 30) {
        NSArray *contexts = [[NSArray alloc] initWithObjects:@"",nil];
        NSArray *names = [[NSArray alloc] initWithObjects:@"main",nil];
        [WKInterfaceController reloadRootControllersWithNames:names contexts:contexts];
    } else {
        [label setText:[NSString stringWithFormat:@"%@",time]];
    }

答案 1 :(得分:-1)

如果手表保持控制器加载,

awakeWithContext将不会再次被调用,这可能是在初始加载后它没有得到更新的原因。

相反,在PageInterfaceController中使用viewWillActivate就像在主InterfaceController中一样,如下所示:

- (void)viewWillActivate {
    [super viewWillActivate];
    [label setText:[context objectForKey:@"time"]];
}
相关问题