Transfer data from WKInterfaceTable to another WKInterfaceController

195 views Asked by At

I am not familiar with transferring or passing data between two WKInterfaceController in Apple Watch. What I am trying to do is , I have some variable like name and age in SecondInterfaceController so I need to pass some value to them from WKInterfaceTable when user tap a row . here is the code :

- (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];

}

but I don't know how can I access to the dictionary from SecondIntefaceController and pass it to the _name (WKInterfaceLable) .

1

There are 1 answers

2
dan On BEST ANSWER

When you push your SecondInterfaceController it will have awakeWithContext: called on it and the context parameter will be the dictionary you passed. Then you can pull the name value out of the context dictionary and assign it to your label.

- (void)awakeWithContext:(id)context {
    NSDictionary *dict = (NSDictionary *)context;
    [_name setText:dict[@"kName"]];
}

You can pass multiple values by adding multiple keys and values to your dictionary.

NSDictionary *d = @{@"kName" : name, @"kAge" : age};
[self pushControllerWithName:@"SecondInterfaceController" context:d];