KVO和ARC如何removeObserver [英] KVO and ARC how to removeObserver

查看:109
本文介绍了KVO和ARC如何removeObserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 ARC 下从对象中删除观察者?我们只是添加观察者,忘记删除它吗?

How do you remove an observer from an object under ARC? Do we just add the observer and forget about removing it? If we no longer manage memory manually where do we resign from observing?

例如,在视图控制器上:

For example, on a view controller:

[self.view addObserver:self
            forKeyPath:@"self.frame"
               options:NSKeyValueObservingOptionNew 
               context:nil];

以前,我会调用 removeObserver:

推荐答案

您仍然可以实现 -dealloc 在ARC下,它似乎是删除关键值的观察值的适当位置。您只需不再在此方法中调用 [super dealloc]

You still can implement -dealloc under ARC, which appears to be the appropriate place to remove the observation of key values. You just don't call [super dealloc] from within this method any more.

如果您覆盖 -release 之前,你做错了方法。

If you were overriding -release before, you were doing things the wrong way.

这篇关于KVO和ARC如何removeObserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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