删除iOS 5 ARC中的NSNotificationCenter观察器 [英] Removing a NSNotificationCenter observer in iOS 5 ARC

查看:146
本文介绍了删除iOS 5 ARC中的NSNotificationCenter观察器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于iOS 5 ARC的项目,我有困难,我应该删除观察员的 NSNotificationCenter 观察,我已在 UIViewController 。 SO类似的帖子说这应该在 -dealloc 方法中完成。即使在ARC项目中不需要此方法,我已使用以下代码添加它:

I have an iOS 5 ARC-based project, and am having difficulty about where I should be removing the observer for the NSNotificationCenter observations which I have registered within a UIViewController. Similar posts on SO have said this should be done in the -dealloc method. Even though this method is not required in ARC projects I have added it with the following code:

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

作为测试,我打开 UIViewController (在 UINavigationController 中),做一些触发通知的事情,然后通过点击后退按钮将其从堆栈中弹出。然后我重新打开 UIViewController ,并做一些更多的事情来触发通知,但注意每个回调被调用两次 - 表明先前的通知没有被取消注册。重复此过程只会导致每个回调被多次调用,因此它们似乎永远不会注销。

As a test, I open the UIViewController (within a UINavigationController), do some things which trigger the notifications, and then pop it off the stack by tapping the Back button. I then reopen the UIViewController, and do some more things to trigger the notifications, but notice that each callback is being called twice - an indication that the previous notifications have not been deregistered. Repeating this procedure just causes each callback to be called more than more times, so they appear to never be deregistering.

任何帮助将不胜感激!

推荐答案

如果你的dealloc没有被调用,很可能是因为有人仍然持有对视图控制器的引用。也许你需要标记一些 __ weak ?您可以使用分配工具来帮助跟踪视图控制器上保存的内容。

If your dealloc isn't being called, it's likely because someone is still holding a reference to the view controller. Perhaps you need to mark something as __weak? You can use the allocations instrument to help track down what's holding on to your view controller.

这篇关于删除iOS 5 ARC中的NSNotificationCenter观察器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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