Reputation: 139
When my application is interrupted, such as receiving a phone call, screen locked, or switching applications, I need it to respond differently depending on which view/viewcontroller is on screen at the time of the interruption.
in my first view controller, we'll call it VCA, I have this
[[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 screen, or any subsequent view controller (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 the UIApplicationWillResignActiveNotification in one of the view controllers that comes after VCA, it will still respond to the doSomethign method defined in VCA.
Is there any way I can make my application respond differently depending on which view is on screen when it goes into the background?
Upvotes: 4
Views: 737
Reputation: 7819
How about you check the current visibleViewController when you received the notification? If it matches with your receiver than perform the action(s), otherwise ignore it.
Upvotes: 0
Reputation: 24248
This works for me in applicationDidEnterBackground
if ([navigationViewController.visibleViewController isKindOfClass:[YourClass class]]) {
//your code
}
Upvotes: 2
Reputation: 37600
Are you saying your doSomethingElse function is never called? Are you sure of this, maybe it is getting called in addition to doSomething? I think so.
In which case in doSomething and doSomethingElse you could add a check as the first line to ignore the notification if not currently loaded:
if ([self isLoaded] == NO)
return;
Upvotes: 0