OC通知实例
#import "NotificationClass.h"
#import "AnotherNotificationClass.h"
@implementation NotificationClass
//注册通知
-(void)addNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFun:) name:@"change" object:nil];
}
-(void)changeFun:(NSNotification *)sender
{
NSDictionary *getDic=[sender userInfo];
NSLog(@"NotificationClass 接收到通知事件,传递的值:%@",getDic);
}
//发送通知
-(void)postNotification
{
NSDictionary *dictionary=[NSDictionary dictionaryWithObject:@"newValue" forKey:@"obj"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:self userInfo:dictionary];//发送给自己
if (!another) {
another=[[AnotherNotificationClass alloc]init];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:another userInfo:dictionary];//发送给another
}
//移除通知
-(void)removeNotification
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end