从docs:
重要通知中心不保留其观察员, 因此,你必须确保注销观察员(使用 removeObserver:或removeObserver:名称:对象:)他们是前 解除分配。 (如果你不这样做,你会如果 中心将消息发送到释放对象生成运行时错误。)
如果你的子类的按钮,那么你可以发布一条消息给所有的观察者的可观察的UIButton即将出现。
[[NSNotificationCenter defaultCenter] postNotificationName:@"UIButton_dealloc" object:self];
或者,在分配的按钮,一旦按钮将被删除,你可以在类:
[[NSNotificationCenter defaultCenter] postNotificationName:@"UIButton_dealloc" object:theButton];
和观察的对象,在这两种情况下,将做到这一点:
// The special event
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:@"UIButton_event" object:theButton];
// The dealloc
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopObserving:) name:@"UIButton_dealloc" object:theButton];
....
-(void) stopObserving:(NSNotification*)notif {
if ([name isEqualToString:@"UIButton_dealloc"]) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIButton_event" object:object];
}
}
但是,在UIButton的情况下,这是一个有点令人费解的例子,但可以用于其他情况。
我不知道这将如何映射到我目前使用的控件事件。要添加观察者,我目前使用'addTarget',所以如果我'addTarget'我不需要也'removeTarget'?无论哪种方式,是一个数组来跟踪最佳方法? – Echilon 2012-03-11 14:10:51
我认为它会,请参阅http://cocoawithlove.com/2008/06/five-approaches-to-listening-observing.html观察员信息 – 2012-03-11 15:51:51
这仍然没有真正回答如何删除添加到buttn的目标对于UIControlEvent。我将作为参数传递给'removeObserver'的是什么? – Echilon 2012-03-12 11:51:58