How to respond to applicationWillResignActive differently from different View controllers

When my application is interrupted, for example, when I receive a phone call, lock the screen or switch applications, I need it to react differently, depending on which view / view on the screen is displayed during the interrupt.

in my first view controller, we will call it VCA, I have it

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething) name:UIApplicationWillResignActiveNotification object:NULL]; -(void)doSomething{ //code here }; 

In VCB, I have

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomethingElse) name:UIApplicationWillResignActiveNotification object:NULL]; -(void)doSomethingElse{ //code here }; 

but if VCB is on the screen or any subsequent controller of the form (vcc, vcd, vce) and the screen is locked, it will only respond to the doSomething method defined in VCA. Even if I don't have UIApplicationWillResignActiveNotification in one of the view controllers that comes after the VCA, it will still respond to the doSomethign method defined in the VCA.

Is there any way to make the application react differently, depending on what kind of view is on the screen when it goes into the background?

+4
source share
3 answers

This works for me in applicationDidEnterBackground

 if ([navigationViewController.visibleViewController isKindOfClass:[YourClass class]]) { //your code } 
+2
source

Are you saying that your doSomethingElse function is never called? Are you sure about this, maybe it is called in addition to doSomething? I think so.

In this case, in doSomething and doSomethingElse, you can add a check as the first line to ignore the notification if it is not loaded:

 if ([self isLoaded] == NO) return; 
0
source

How about checking the current visibleViewController when you received a notification? If it matches your receiver, than perform the action (s), otherwise ignore it.

0
source

All Articles