2011-08-15 75 views
0

我送使用的通知:NSNotification是否可以在任何地方使用?

[[NSNotificationCenter defaultCenter] postNotificationName:@"historyLoaded" object:jsonReturn]; 

而接收到该通知使用:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(manageHistory:) name:@"historyLoaded" object:nil]; 

然后在选择器中的方法是:

- (void) manageHistory: (NSNotification *) historyData{ 
    NSLog(@"this bit of code was run"); 
} 

对于一些reaason通知没有通过。可以在应用程序的任何位置发送和接收通知吗?

回答

1

postNotification中的object参数应填写“发送”通知的对象,或者如果发送者未必指定,则填充nil。
如果你想传递一些信息,你应该使用postNotificationName:object:userInfo,并把信息放在userInfo字典中。

0
[[NSNotificationCenter defaultCenter] addObserver:self 
     selector:@selector(manageHistory) name:@"historyLoaded" object:nil]; 

[[NSNotificationCenter defaultCenter] postNotificationName:@"historyLoaded" 
     object:nil userInfo:jsonReturn]; 

- (void) manageHistory: (NSNotification *) historyData{ 
     NSDictionary* _dict = historyData.userInfo; 
     NSLog(@"Your information embedded to dictiuonary obj %@",_dict); 
} 

注意:请确保您的historyData应该是一个字典对象postNotificationName

+0

我有上面写的应该为你工作的代码。 –

相关问题