//设置通知
//获取通知中心
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
];
}
最后
以上就是从容大地最近收集整理的关于iOS 通知的基本使用传值的全部内容,更多相关iOS内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复