dealloc上是否需要removeObserver? [英] is removeObserver necessary on dealloc?

查看:141
本文介绍了dealloc上是否需要removeObserver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个视图控制器中,它将自身添加为UITextViewTextDidEndEditingNotification通知的观察者,如下所示

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];


推荐答案

是的,你应该随时删除任何观察者被解除了。否则通知中心将继续引用现有的dealloc'd对象,并继续尝试向他们转发通知。

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.

这篇关于dealloc上是否需要removeObserver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆