0

我正在使用以下方式发送通知:

[[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");
}

由于某种原因,通知没有通过。可以从应用程序的任何位置发送和接收通知吗?

4

2 回答 2

1

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

于 2011-08-15T10:51:47.687 回答
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 中的字典对象

于 2011-08-15T11:04:21.513 回答