0

我不熟悉 WKInterfaceController在 Apple Watch 中的两个之间传输或传递数据。我想做的是,我有一些变量name,所以我需要在用户点击一行时将一些值传递给age它们。这是代码:SecondInterfaceControllerWKInterfaceTable

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {

    NSString *name;
    NSString *age;

    switch (rowIndex) {
        case 0:
            name = @"Jack";
            age = @"22";
            break;

        default:
            break;
    }

    NSDictionary *d = [NSDictionary dictionaryWithObject:name forKey:@"kName"];
    [self pushControllerWithName:@"SecondInterfaceController" context:d];

}

但我不知道如何从 SecondIntefaceController 访问字典并将其传递给 _name (WKInterfaceLable) 。

4

1 回答 1

1

当您推送SecondInterfaceController时,它将awakeWithContext:调用它,context参数将是您传递的字典。然后,您可以从上下文字典中提取名称值并将其分配给您的标签。

- (void)awakeWithContext:(id)context {
    NSDictionary *dict = (NSDictionary *)context;
    [_name setText:dict[@"kName"]];
}

您可以通过向字典添加多个键和值来传递多个值。

NSDictionary *d = @{@"kName" : name, @"kAge" : age};
[self pushControllerWithName:@"SecondInterfaceController" context:d];
于 2015-06-19T14:42:13.713 回答