【发布时间】:2011-09-22 02:09:34
【问题描述】:
我有一个客观的 C 类。在其中,我创建了一个 init 方法并在其中设置了一个 NSNotification
//Set up NSNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getData)
name:@"Answer Submitted"
object:nil];
在这个类中我在哪里设置[[NSNotificationCenter defaultCenter] removeObserver:self]?我知道对于一个UIViewController,我可以将它添加到viewDidUnload方法中那么如果我只是创建了一个目标c类需要做什么呢?
【问题讨论】:
-
我放在dealloc方法里。
-
dealloc方法在我创建objective c类的时候没有自动为我创建,所以我添加进去就可以了?
-
是的,您可以实现
-(void)dealloc,然后在其中添加removeObserser:self。这是最推荐的放removeObservers:self的方式 -
在iOS 6中放入
dealloc方法还可以吗? -
是的,在ARC项目中使用dealloc是可以的,只要不调用[super dealloc](调用[super dealloc]会出现编译错误)。是的,你绝对可以把你的 removeObserver 放在 dealloc 中。
标签: objective-c ios nsnotifications