Anthony
Anthony

Reputation: 2801

How to close a modal view when the application enter in background on ios

I have a modal view created in a method (there is no reference in the mainview) and I want to do a dismissModalViewControllerAnimated automatically when my app enter in background. How can I do that ?

Upvotes: 3

Views: 5684

Answers (2)

greenhorn
greenhorn

Reputation: 1107

In the mainview's viewDidLoad, add observer to be notified when app goes to background.

- (void) viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
        selector:@selector(goToBackground) 
        name:UIApplicationWillResignActiveNotification object:nil];
}

Define the function goToBackground(). It will be called when the app goes to background

- (void) goToBackground
{
    [self dismissModalViewControllerAnimated: NO]; // no need to animate 
}

Don't forget to remove the observer

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

Upvotes: 9

Gabriel
Gabriel

Reputation: 3359

You can use a notification. Post a notification from the ApplicationDelegate's method applicationDidEnterBackground:. YOu can call the dismiss method from the modal controller, so add it as observer to the notification center.

Upvotes: 1

Related Questions