简体   繁体   English

如何使用[self pushControllerWithName:xxx context:NSdictionary]?

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

Working with WatchOS: 使用WatchOS:

In InterfaceContoller.m 's (void)session .... didReceiveMessage ... 在InterfaceContoller.m的(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 在Controller.m的-(void)awakeWithContext:(id)context

[super awakeWithContext:context];

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

I got context = <InterfaceController: 0x14d80a30> instead of the NSObject I set. 我得到context = <InterfaceController: 0x14d80a30>而不是我设置的NSObject。

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. 我想做的是,在pushController方法中传递一个值,然后Controller.m将检查该值并根据该值执行不同的操作。 How do I achieve that? 我该如何实现?

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. 我收到了该答复,因为我已经放置了context:self ,并且我使用的是该代码,而不是问题中所述的NSDictionary。

Alternatively, since the context is an ID, we can check the ID for the class we want (NSDictionary, NSString, NSNumber etc) by doing so: 或者,由于context是ID,因此我们可以通过以下方法检查所需类的ID(NSDictionary,NSString,NSNumber等):

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 参考: 目标C-如何对ID进行字符串化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM