0

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) .

iOS.Lover
  • 5,923
  • 21
  • 90
  • 162

1 Answers1

1

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];
dan
  • 9,695
  • 1
  • 42
  • 40
  • Thank you ! but what about passing different dictionaries !? I if add `dinoDict = [NSDictionary dictionaryWithObject:age forKey:@"kAge"];` only one dictionary will be passed , how can I attach multiple value to the one context ? – iOS.Lover Jun 19 '15 at 15:14
  • 1
    @Mc.Lover I added some code to my answer to show how to pass multiple values in the dictionary – dan Jun 19 '15 at 15:18