您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 通知 notification