1.发布通知
NSDictionary *dic_info = @{
@"name":@"water",
@"sex":@"man",
@"age":@"22"
};
/* self:发送通知的对象
* dic_info:通知的内容
*/
/* 创建通知
*/
NSNotification *note = [NSNotification notificationWithName:@"通知的标题" object:self userInfo:dic_info];
/* 发送通知
*/
[[NSNotificationCenter defaultCenter] postNotification:note];
2.监听通知
/* 监听通知
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:@"通知的标题" object:nil];
3.监听到通知以后,做一些处理
-(void)doSomething:(NSNotification*)note
{
/* 监听到通知以后,做一些反应,比如通知吃饭去了,然后就要收拾东西,准备吃饭
*/
NSLog(@"doSomething");
}
4.移除通知
-(void)dealloc
{
/* 移除通知
*/
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
网友评论