在UIViewController中向NSNotificationCenter添加和删除观察者 [英] Adding and removing observers to NSNotificationCenter in a UIViewController

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

问题描述

查看各种Apple示例(例如添加音乐),其中我看到他们将观察者添加到viewDidLoad中默认的NSNotificationCenter,然后在dealloc中将其删除.这似乎很危险,因为可以多次调用viewDidLoad而不调用dealloc.然后,这将多次添加同一观察者,从而导致多次调用该处理程序.

Looking at various Apple examples (for example Add Music) in which I see they add observers to the default NSNotificationCenter in viewDidLoad, then remove them in dealloc. This seems dangerous as viewDidLoad can be called multiple times without dealloc being called. This would then add the same observer multiple times, causing the handler to be called multiple times.

对此的一种解决方案是也删除viewDidUnload中的观察者,但这将意味着同一观察者可以在dealloc中第二次被除去,这似乎是一个潜在的问题.

A solution to this would be to also remove observers in viewDidUnload, but this would then mean the same observer could be removed for a second time in dealloc which seems like a potential problem.

我想念什么?

推荐答案

关于正确删除通知的讨论很多.例如:

There are a lot of discussions about removing notifications in the right way. For example:

  • removeobserver-with-nsnotification-what-am-i-doing-wrong
  • removing-a-nsnotificationcenter-observer-in-ios-5-arc

我建议您删除viewWillDisappear(或viewDidDisappear)和viewDidUnload生命周期方法中的观察者. (注意: viewDidUnload已过时,不应在iOS6 +中实现;请参见

I suggest you to remove observers in viewWillDisappear (or viewDidDisappear) and viewDidUnload lifecycle methods. (Note: viewDidUnload was deprecated and shouldn't be implemented in iOS6+; see iOS 6 - viewDidUnload migrate to didReceiveMemoryWarning?)

重要提示:

viewDidUnload不能保证被调用-这不是标准的生命周期方法.

viewDidUnload is not guaranteed to be called - it's not a standard lifecycle method.

来自Apple文档:

viewDidUnload 当内存不足的情况发生并且当前视图控制器的 不需要视图,系统可能会选择从中删除这些视图 记忆.在视图控制器的视图具有 已发布,您有机会进行最后的清理.

viewDidUnload When a low-memory condition occurs and the current view controller’s views are not needed, the system may opt to remove those views from memory. This method is called after the view controller’s view has been released and is your chance to perform any final cleanup.

相反,只要该接收者的引用数为零,就会调用dealloc.

Instead, dealloc is called whenever the number of references for that receiver is zero.

希望有帮助.

修改

为了完整起见,您可以查看此链接,了解如何

For the sake of completeness you can see this link on how to avoid-nsnotification-removeobserver. The link provide some useful guidelines to remove observer (see also the comments). The author does it in viewDidAppear/viewDidDisappear methods since viewWillAppear and viewWillDisappear are not always called correctly in many applications. It's your choice.

如果您想确保以正确的方式删除观察者,请在dealloc方法中注销它,或者在第二条注释中编写的视图完全卸载后,再进行注销. 但是请确保将来会调用dealloc.换句话说,正如我已经提到的,如果控制器由于其他对象已被引用而继续保持活动状态,则该方法将永远不会被调用.在这种情况下,控制器将继续接收通知.

If you want to be sure to remove observers in the right way unregister it in dealloc method or when the view is fully unloaded as you wrote in the second comment. But be sure that dealloc will be call in the future. In other words, as I already mentioned, if the controller continues to stay alive since some other object has a referenced to it, the method will never get called. In this case the controller continues to receive notifications.

这篇关于在UIViewController中向NSNotificationCenter添加和删除观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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