您的位置:首页 > 其它

NSNotification(通知)

2016-04-13 10:16 519 查看
//创建一个消息对象
NSNotification * notice = [NSNotification notificationWithName:@"notification" object:nil userInfo:@{@"key":@"value"}];
//发送消息
[[NSNotificationCenter defaultCenter]postNotification:notice];


//获取通知中心单例对象
NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
//添加当前类对象为一个观察者,name和object设置为nil,表示接收一切通知
[center addObserver:self selector:@selector(notice:) name:@"notification" object:nil];


-(void)notice:(NSNotification *)sender{
NSLog(@"%@",sender.userInfo[@"key"]);
}


//释放通知
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: