我有一个客观的C类.在其中,我创建了一个init方法并在其中设置了NSNotification
//Set up NSNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getData)
name:@"Answer Submitted"
object:nil];
Run Code Online (Sandbox Code Playgroud)
我在哪里设置[[NSNotificationCenter defaultCenter] removeObserver:self]这门课程?我知道,对于a UIViewController,我可以将它添加到viewDidUnload方法中如果我刚刚创建了一个目标c类,需要做什么?
我有一个基于iOS 5 ARC的项目,并且在我应该删除观察者的位置时遇到困难,NSNotificationCenter因为我已经注册了UIViewController.关于SO的类似帖子已经说过这应该在-dealloc方法中完成.尽管ARC项目中不需要此方法,但我已使用以下代码添加它:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Run Code Online (Sandbox Code Playgroud)
作为测试,我打开UIViewController(在a内UINavigationController),做一些触发通知的事情,然后通过点击Back按钮将其弹出堆栈.然后UIViewController,我重新打开,并做更多事情来触发通知,但请注意每个回调被调用两次 - 表明以前的通知尚未注销.重复此过程只会导致每次回调被调用多次,因此它们似乎永远不会取消注册.
任何帮助,将不胜感激!
cocoa-touch objective-c nsnotifications nsnotificationcenter ios