dealloc 中是否需要removeObserver?

发布于 2024-12-23 07:18:47 字数 366 浏览 3 评论 0原文

在我的一个视图控制器中,它将自身添加为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城已无爱 2024-12-30 07:18:47

是的,当观察者被释放时,你应该总是删除它们。否则,通知中心将保留对周围已释放对象的引用,并继续尝试将通知转发给它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文