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

标签 ios objective-c iphone watchkit wkinterfacetable

我不熟悉在两个 WKInterfaceController 之间传输或传递数据在苹果 watch 中。我想做的是,我有一些变量,如 nameageSecondInterfaceController所以我需要从 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/

相关文章:

ios - Swift 应用内购买非消耗品

objective-c - 使用 AVFoundation 修改音频元数据

iPhone SDK 4.3 libav编译问题

iphone - NSURLConnection 忽略 Keep-Alive 超时?

ios - 使用 UITableView 时 tableView :numberOfRowsInSection . ..NSInvalidArgumentException

IOS如何快速删除核心数据中的对象

ios - 使用 self.delegate 子类化 UITextField 会导致应用程序卡住,CPU 峰值达到 100%

objective-c - 在类中使用枚举和 switch 语句

objective-c - ARC - __unsafe_unretained 的含义?

iphone - 是否有可能(如何)让应用程序在 iPhone 的聚光灯搜索中返回自定义搜索结果?