我不熟悉在Apple Watch中的两个WKInterfaceController
之间传输或传递数据。我想要做的是,我在name
中有一些变量,例如age
和SecondInterfaceController
,所以我需要在用户点按一行时从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)。
答案 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];