Ios 无法移除观察者?
我已经开始学习iOS开发几个星期了,但是这个bug让我很困惑 我只是在ViewController中发布了4个不同的观察者:(他们有不同的通知名称) 然后我想在另一个ViewController中添加和删除4个不同的观察者,如下所示:Ios 无法移除观察者?,ios,notifications,observer-pattern,Ios,Notifications,Observer Pattern,我已经开始学习iOS开发几个星期了,但是这个bug让我很困惑 我只是在ViewController中发布了4个不同的观察者:(他们有不同的通知名称) 然后我想在另一个ViewController中添加和删除4个不同的观察者,如下所示: -(void)viewwillappear{ self.localChangeObserver=[[NSNotificationCenter defaultCenter]addObserverForName:@"notificationame"
-(void)viewwillappear{
self.localChangeObserver=[[NSNotificationCenter defaultCenter]addObserverForName:@"notificationame" object:nil queue:nil usingBlock:^(NSNotification *note) { }];
}
-(void)viewwilldisappear{
[[NSNotificationCenter defaultCenter]removeObserver:self.localChangeObserver];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
但这个“removeObserver”却不起作用。每次我显示这个ViewController时,它都会添加一个观察者(然后我隐藏这个VC,什么都没有删除)。所以我最终得到了很多观察者
而且,将它们放在ViewDidLoad/dealloc中也不起作用
但是,另外两个观察者工作正常。如下所示:
-(void)viewwillappear{
self.localChangeObserver=[[NSNotificationCenter defaultCenter]addObserverForName:@"notificationame" object:nil queue:nil usingBlock:^(NSNotification *note) { }];
}
-(void)viewwilldisappear{
[[NSNotificationCenter defaultCenter]removeObserver:self.localChangeObserver];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
我真的不知道我的代码有什么问题。非常感谢。试试下面的代码
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"notificationame" object:nil];
使用以下代码:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserverForName:@"OBSERVER NAME" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *aNotification)
{
//Write your Notification handler Code
}];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"OBSERVER NAME" object:nil];
}
此代码工作正常 如果这是您的真实代码,那么问题在于方法名称<代码>视图将消失应为
视图将消失
。在每个方法中放置一个NSLog
,并确保在您期望的时候调用它。感谢您的帮助..但它仍然不起作用。它可以成功添加观察者,但无法删除观察者。每次我进入这个ViewController,观察者越来越多,然后每一个通知后的线索,然后每一个通知后的线索,使块被再次执行和agian。请删除您在问题中提到的带有其名称的单个观察者。[[NSNotificationCenter defaultCenter]removeObserver:self name:@“UIKeyboardWillShowNotification”对象:nil];[[NSNotificationCenter defaultCenter]removeObserver:self name:@“UIKeyboardWillHideNotification”对象:nil];虽然这段代码不起作用,但你的建议确实对我有很大帮助。最后我发现,如果我使用[addObserverForName:…]这总是无法删除,但如果我使用[addObserver:self selector:@selector:…]它可以删除。我想这是因为[postNotificationName:]只能用于后者。谢谢你的帮助,但它仍然不起作用……也许我应该在其他地方找出错误。