removeObserver不适用于UIApplicationWillEnterForegroundNotification IOS

时间:2014-06-02 06:06:30

标签: iphone nsnotificationcenter

您好我正在开发iPhone应用程序,我正在注册UIApplicationWillEnterForegroundNotification以进行通知。但是当我试图取消注册那个观察者时,它就无法工作了。它仍然被调用。我的代码看起来像

在viewDidLoad方法中我正在注册

  [[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(applicationBecomeActive)
 name:UIApplicationWillEnterForegroundNotification
 object:nil];

在我的注销方法中,我试图像这样删除这个观察者

 [[NSNotificationCenter defaultCenter] removeObserver:UIApplicationWillEnterForegroundNotification];

但这不起作用。难道我做错了什么。需要一些帮助。谢谢。

1 个答案:

答案 0 :(得分:6)

那是因为你没有删除观察者。

注意你的添加观察者方法:

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(applicationBecomeActive)
 name:UIApplicationWillEnterForegroundNotification
 object:nil];

重要的部分是addObserver:self

因此,为了删除观察者,你可以:

[[NSNotificationCenter defaultCenter] removeObserver:self];

或者如果您只想删除特定通知名称:

[[NSNotificationCenter defaultCenter] removeObserver:self 
       name:UIApplicationWillEnterForegroundNotification object:nil];
相关问题