Remove notification observer from another view controller

一曲冷凌霜 提交于 2020-01-05 10:36:43

问题


Hi I am developing Iphone application in which am registering one notification observer for UIApplicationWillEnterForegroundNotification. Now I want to remove that one from another view controller. My code looks like

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

And I am creating one method for removing observer:

-(void) removeObserver
{
  [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

and I am calling this method from other view controller but its not working. I think I have to store observer. But I don't know how to do this. Need help. Thank you.


回答1:


The second view controller needs a reference to the instance of the first view controller. Let's assume it is hold in a property:

@property (nonatomic, strong) FirstViewControler *firstViewController;

Then your code to remove the fist view controller as an observer would look like this:

- (void)removeObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self.firstViewController]; 
}

The missing part is: You have to set the property somewhere. Where to do that depends on your code.



来源:https://stackoverflow.com/questions/23998145/remove-notification-observer-from-another-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!