Search code examples
iosobjective-cxcodewatchkit

How to use [self pushControllerWithName:xxx context:NSdictionary]?


Working with WatchOS:

In InterfaceContoller.m 's (void)session .... didReceiveMessage ...

NSDictionary *request = @{@"testkey":@"testvalue"};
//i've also tried context:[NSNumber numberWithInt:1]
[self pushControllerWithName:@"Controller" context:request];

In Controller.m's -(void)awakeWithContext:(id)context

[super awakeWithContext:context];

NSLog(@"context = %@", context);

I got context = <InterfaceController: 0x14d80a30> instead of the NSObject I set.

What I wanted to do is, pass a value during the pushController method, and Controller.m will check for the value and perform different action base on the value. How do I achieve that?


Solution

  • Sorry guys, it was a mistake on my end.

    I got that reply because I've put the context:self, and i was using that code instead of the NSDictionary stated in the question.

    Alternatively, since the context is an ID, we can check the ID for the class we want (NSDictionary, NSString, NSNumber etc) by doing so:

    if ([context isKindOfClass:[NSDictionary class]])
    //[NSDictionary class] to check for NSDictionary. Use NSString, NSNumber for the one you need etc
    {
        NSDictionary* dict = (NSDictionary*)context;
        NSLog(@"context dict = %@", dict);
    }
    

    Silly me!

    Reference: Objective C - How to Stringify an id