将数据从WKInterfaceTable传输到另一个WKInterfaceController

时间:2015-06-19 14:27:20

标签: ios objective-c iphone watchkit wkinterfacetable

我不熟悉在Apple Watch中的两个WKInterfaceController之间传输或传递数据。我想要做的是,我在name中有一些变量,例如ageSecondInterfaceController,所以我需要在用户点按一行时从WKInterfaceTable传递一些值。这是代码:

- (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)。

1 个答案:

答案 0 :(得分: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];