场景:一般为model层对,controller和view进行的通知方式,不关心谁去接收,只负责发布信息。
功能:既可传值又可传对象
注意:
1. 使用后要在delloc中释放;
2. 在传字典的时候需注意,字典中不能定义变量;
3. addNotification 要放在 PostNotifacation 提前申明;
具体用法:
[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil userInfo:nil];
//第二步:在需要的地方添加观察者用来接收消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action:) name:@"tongzhi" object:nil];
-(void)action:(NSNotification *)noti
{
NSDictionary *dic=noti.userInfo;
NSLog(@"------------%@-------%@",dic[@"name"],dic[@"age"]);
Person *pson= (Person *)noti.object;
NSLog(@"-----------%@--------%d",pson.name,pson.age);
}
//第三步:释放消息
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"tongzhi" object:nil];
}