简体   繁体   English

观察者永远不会从NSNotificationCenter中删除

[英]Observer never gets removed from NSNotificationCenter

I'm adding a view controller as an observer for UIKeyboardWillShowNotification notification. 我正在添加一个视图控制器作为UIKeyboardWillShowNotification通知的观察者。

I have this code in my viewDidLoad : 我在viewDidLoad有这个代码:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWillShow:)
                                         name:UIKeyboardWillShowNotification
                                       object:nil];

And in my dealloc : 在我的dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self];

The observer is not being removed even though dealloc is called when the view controller is closed. 即使在视图控制器关闭时调用dealloc也不会删除观察者。 So when I open it for the second time, NSNotificationCenter will attempt to notify the old object, which is released, and the app crashes. 因此,当我第二次打开它时,NSNotificationCenter将尝试通知已发布的旧对象,并且应用程序崩溃。

I have seen several questions here on StackOverflow about this particular problem, but non of the answers works for me. 我在StackOverflow上看到了关于这个特殊问题的几个问题,但没有答案对我有用。

I've tried removing the observer in viewWillDisappear and viewDidDisappear but the same problem happens. 我已经尝试在viewWillDisappearviewDidDisappear删除观察者,但同样的问题发生了。

I'm using ARC. 我正在使用ARC。

Have you tried this exact chunk of code in viewWillDisappear ? 你有没有在viewWillDisappear尝试过这段精确的代码?

- (void)viewWillDisappear:(BOOL)animated 
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

From your explanation I don't think the problem is with the removal of the observer. 根据你的解释,我认为问题不在于移除观察者。 Try to trigger the Observer from another viewcontroller. 尝试从另一个viewcontroller触发Observer。 If it's not triggered you'll know the removal is successful and the problem occurs when you are adding the observer the second time. 如果未触发,您将知道删除成功,并且在第二次添加观察者时会出现问题。

也许尝试通过指定之前设置的参数name ,如下所示:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

Looks like the observer is getting set multiple times. 看起来观察者已多次设置。 Is your controller inherited from a class which also registers for same notification? 您的控制器是否继承了同样注册相同通知的类? That could lead to controller instance getting registered as observer more than once. 这可能导致控制器实例多次注册为观察者。 As a workaround try this, in your controller class where you add the observer, 作为一种解决方法尝试这个,在你添加观察者的控制器类中,

// Remove as observer first
[[NSNotificationCenter defaultCenter] removeObserver:self];
                                      name:UIKeyboardWillShowNotification
                                      object:nil];
// Then add
[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(keyboardWillShow:)
                                      name:UIKeyboardWillShowNotification
                                      object:nil];

This will ensure that the observer is getting added only once. 这将确保观察者只被添加一次。

Hope that helps! 希望有所帮助!

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"name" object:nil];

它适用于我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM