【问题标题】:Transfer data from WKInterfaceTable to another WKInterfaceController将数据从 WKInterfaceTable 传输到另一个 WKInterfaceController
【发布时间】:2015-06-19 14:27:20
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c iphone watchkit wkinterfacetable


    【解决方案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];
    

    【讨论】:

    • 谢谢!但是传递不同的字典呢!?如果添加dinoDict = [NSDictionary dictionaryWithObject:age forKey:@"kAge"]; 只会传递一个字典,如何将多个值附加到一个上下文?
    • @Mc.Lover 我在答案中添加了一些代码来展示如何在字典中传递多个值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2013-07-04
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多