#define QuitToDeleteCache @"QuitToDeleteCache"
//注册通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updatePredictor:) name:QuitToDeleteCache object:nil];
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:QuitToDeleteCache object:nil];
//发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:QuitToDeleteCache object:nil userInfo:nil];
如果传递的参数有多个可以通过
[[NSNotificationCenter defaultCenter] postNotificationName : NOTIF_DataComplete object:nil userInfo: nil];userInfo后面跟的是一个NSDictionary,我们可以将要传递的多个参数都装到NSDictionary然后进行传递。取出来的时候只要通过notification.userInfo来取得这个字典。
重要提示:应用这种模式需要注意的是消息中心对消息的分发是同步的,也就是说消息在发送到下一个接收者之前需要前一个接受者快速返回,否则就会阻塞下一个接收者消息,所以对于长时间的消息处理,需要另外启动一个线程来单独处理消息接收后的任务,确保在接收到消息以后立即返回。
//传参
{
NSMutableDictionary* faDictonary = [NSMutableDictionarydictionary];
;
[faDictonary
setValue:[NSNumber
numberWithInt: 112]
forKey:@"Number_112"];
[faDictonary
setValue:selectArray forKey:@"Array_selecte"];
[[NSNotificationCenterdefaultCenter]postNotificationName:QuitToDeleteCacheobject:niluserInfo:faDictonary];
}
//取出
-(void)updatePredictor:(NSNotification*)notification
{
NSNumber *nWhich = [notification.userInfoobjectForKey:@"Number_112"];
NSMutableArray* array = [notification.userInfoobjectForKey:@"Array_selecte"];
NSLog(@"%d",[nWhich
intValue]);
for (int i =
0; i<[array count]; i++) {
NSLog(@"%@",[array
objectAtIndex:i]);
}
}