OC中通知使用浅析

来源:互联网 发布:拷贝文件 linux 编辑:程序博客网 时间:2024/06/06 16:59

注册发送通知

//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


1 0
原创粉丝点击