将数据从 WKInterfaceTable 传输到另一个 WKInterfaceController
Transfer data from WKInterfaceTable to another WKInterfaceController
我不熟悉在 Apple Watch 中的两个 WKInterfaceController
之间传输或传递数据。我想做的是,我在 SecondInterfaceController
中有一些像 name
和 age
这样的变量,所以当用户点击一行时,我需要从 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];
我不熟悉在 Apple Watch 中的两个 WKInterfaceController
之间传输或传递数据。我想做的是,我在 SecondInterfaceController
中有一些像 name
和 age
这样的变量,所以当用户点击一行时,我需要从 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];