dealloc 中是否需要removeObserver?
在我的一个视图控制器中,它将自身添加为 UITextViewTextDidEndEditingNotification 通知的观察者,如下所示
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(done:) name:UITextViewTextDidEndEditingNotification object:nil];
现在我想知道 - 当视图控制器被释放时我需要执行以下操作
[[NSNotificationCenter defaultCenter] removeObserver:self];
In one of my view controller, it adds itself as observer of UITextViewTextDidEndEditingNotification notification, like the following does
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(done:) name:UITextViewTextDidEndEditingNotification object:nil];
Now I am wondering - do I need to do the following when the view controller is dealloc'd
[[NSNotificationCenter defaultCenter] removeObserver:self];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,当观察者被释放时,你应该总是删除它们。否则,通知中心将保留对周围已释放对象的引用,并继续尝试将通知转发给它们。
yes, you should always remove any observers when they're being dealloc'd. otherwise the notification center will keep references to the now-dealloc'd objects around and continue to try to forward notifications to them.