//设置通知
//获取通知中心
NSNotificationCenter
*nc = [
NSNotificationCenter
defaultCenter];
//通知中心 发送广播
/*
第一个参数就是通知的名字 第二个参数 谁发送的通知
第三个参数 通知的内容
这个方法内部会创建一个通知对象
*/
//把通知的内容放入字典中
NSDictionary
*dict = @{@
"status"
: @
"123"
};
//自定义的通知
[nc postNotificationName:kNotificationChangeStatus object:
self
userInfo:dict];
/***********************************************************************************/
//接收通知
//在viewDidLoad方法中注册观察者
//提前创建观察者
NSNotificationCenter
*nc = [
NSNotificationCenter
defaultCenter];
//注册观察者
//接收任意对象发送的kNotificationChangeStatus通知
[nc addObserver:
self
selector:
@selector
(changeLabelText:) name:kNotificationChangeStatus object:
nil
];
//实现接收通知方法
- (
void
)changeLabelText:(
NSNotification
*)nf{
//获取通知的内容
NSDictionary
*dict = nf.userInfo;
NSString
* isOn = dict[@
"status"
];
NSLog
(@
"%@"
, isOn);
}
//最后别忘了
//删除观察者
- (
void
)dealloc{
//删除观察者
[[
NSNotificationCenter
defaultCenter] removeObserver:
self
name:kNotificationChangeStatus object:
nil
];
}