我不熟悉在两个 WKInterfaceController
之间传输或传递数据在苹果 watch 中。我想做的是,我有一些变量,如 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) 。
最佳答案
当您推送 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];
关于ios - 将数据从 WKInterfaceTable 传输到另一个 WKInterfaceController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30940667/