NSNotificationCenter的作用:用于程序中不同类间的消息通信,使用步骤如下:
1,发送通知,
NSDictionary *objects = @{};//用字典存放发送通知的内容
[[NSNotificationCenter defaultCenter]
postNotificationName:@"idstring" object:self userInfo:objects];
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。( postNotificationName的名字必须唯一)
2,注册通知:即要在什么地方接受消息
[[NSNotificationCenter defaultCenter] a
ddObserver:self selector:@selector(method:)name:@"idString" object:nil];
参数介绍:
addObserver: 观察者,即在什么地方接收通知;
selector: 收到通知后调用的方法;
name: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。(通知的名字要与发送通知的名字一样,当通知发出来以后,观察者就会自动收到通知并调用相 应 的方法处理)
-(void) method :(NSNotification *) notification {
NSDictionary *data= notification.userInfo;//这里取出通知内容
//处理 ....
}
4,移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"idString" object:nil];