NSNotificationCenter 在 init里面注册这个通知,
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(on:) name:str object:nil];
在dealloc里面移除这个通知的注册:
[[NSNotificationCenter defaultCenter] removeObserver:self name:str object:nil];
以上为不带参数的通知
一般在使用NSNotificationCenter的时候不使用参数,但是有些时候需要使用参数。
传递参数
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:searchFriendArray];
接收参数并获取传递的参数
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:传递的参数
- (void) test:(NSNotification*) notification
{
searchFriendArrary = [notification object];//通过这个获取到传递的对象
}
//通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"chuangcan"object:self];
//监听通知
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(chuangcan:) name:@"chuangcan" object:nil];
-(void)chuangcan:(NSNotification *) noti
{
//获取参数
UIViewController temp=[noti object];
}
//通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"chuangcan"object:self];
//监听通知
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(chuangcan:) name:@"chuangcan" object:nil];
-(void)chuangcan:(NSNotification *) noti
{
//获取参数
UIViewController temp=[noti object];
}