OC中通知使用浅析
2016-02-04 13:13
453 查看
注册发送通知
//Refresh_Comment是通知的名字
[[NSNotificationCenterdefaultCenter] postNotificationName:Refresh_Comment object:nil userInfo:@{@"data":data}];
接收通知传过来的值(这个值放到"refreshComment"这个方法中)
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(refreshComment:)name:Refresh_Comment object:nil];
用字典的健把这个值取出来
- (void)refreshComment:(NSNotification *)notification
{
_dataModel.commentCount = ((CircleData *)notification.userInfo[@"data"]).commentCount;
_comments = ((CircleData *)notification.userInfo[@"data"]).comments;
[_tableView reloadData];
}
dealloc移除通知
-(void)dealloc
{
[[NSNotificationCenterdefaultCenter] removeObserver:self];
}
注意:
向NSNotificationCenter中addObserver后,并没有对这个对象进行引用计数加1操作,它只是保存了地址。
在你不是销毁的时候,千万不要直接调用[[NSNotificationCenter defaultCenter] removeObserver:self]; 这个方法,因为你有可能移除了系统注册的通知。
在什么线程发通知,就会在什么线程执行通知的内容.
就是在页面出现的时候注册通知,页面消失时移除通知。一定要成双成对出现。
http://www.cocoachina.com/ios/20150120/10954.html
//Refresh_Comment是通知的名字
[[NSNotificationCenterdefaultCenter] postNotificationName:Refresh_Comment object:nil userInfo:@{@"data":data}];
接收通知传过来的值(这个值放到"refreshComment"这个方法中)
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(refreshComment:)name:Refresh_Comment object:nil];
用字典的健把这个值取出来
- (void)refreshComment:(NSNotification *)notification
{
_dataModel.commentCount = ((CircleData *)notification.userInfo[@"data"]).commentCount;
_comments = ((CircleData *)notification.userInfo[@"data"]).comments;
[_tableView reloadData];
}
dealloc移除通知
-(void)dealloc
{
[[NSNotificationCenterdefaultCenter] removeObserver:self];
}
注意:
向NSNotificationCenter中addObserver后,并没有对这个对象进行引用计数加1操作,它只是保存了地址。
在你不是销毁的时候,千万不要直接调用[[NSNotificationCenter defaultCenter] removeObserver:self]; 这个方法,因为你有可能移除了系统注册的通知。
在什么线程发通知,就会在什么线程执行通知的内容.
就是在页面出现的时候注册通知,页面消失时移除通知。一定要成双成对出现。
http://www.cocoachina.com/ios/20150120/10954.html
相关文章推荐
- android通知栏Notification点击,取消,清除响应事件
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Android编程自定义Notification实例分析
- android中创建通知栏Notification代码实例
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题
- Notification与NotificationManager详细介绍
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 浅析PHP微信支付通知的处理方式
- 详解Android中Notification通知提醒
- 探讨Android与iOS,我们将何去何从?