Reputation: 3350
I'm using the below notifications to reload ViewControllerA
when my app comes back from background mode. It works correctly, but the applicationEnteredForeground:
method gets called every time when I open the app again. For example if I close the app when ViewControllerB
or ViewControllerC
is on the screen and open it again the method will be called despite the viewDidLoad
of ViewControllerB
doesn't contain applicationEnteredForeground:
method. I would like to know that how could solve this issue? My goal is to use applicationEnteredForeground:
only when ViewControllerA
was on the screen before I closed the app.
As a possible solution I would just remove the NSNotificationCenter
in the viewDidDisappear
, but since the observer is in the viewDidLoad
it won't work when the user navigates back, because viewDidLoad
won't be called again. Is there any fix for this?
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationEnteredForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
- (void)applicationEnteredForeground:(NSNotification *)notification {
// do stuff...
}
Upvotes: 0
Views: 587
Reputation: 5563
You can check if a view controller is on screen by checking the window property of it's view. It will work in most standard cases.
- (void)applicationEnteredForeground:(NSNotification *)notification
{
if (self.view.window == nil) {
// Not on screen
return;
}
// do stuff...
}
Upvotes: 1
Reputation: 1139
You should remove ViewController A's event listener on viewWillDisappear and add it in viewWillAppear. That way, VC A will only be listening when it is the visible view controller.
Upvotes: 2