Alex
Alex

Reputation: 4190

Determine if UIViewController is closing due to application exit?

Is there a way to test in viewWillDisappear if it's being called because the application is exiting, versus normal ways of it being dismissed? The method applicationWillTerminate in the App Delegate is called after the current view is closed. I want to do different things depending on whether it's being dismissed due to a IBAction or the user clicking the menu button.

Thanks!

Upvotes: 3

Views: 2637

Answers (2)

Nathan de Vries
Nathan de Vries

Reputation: 15511

You should use observe the UIApplicationWillTerminateNotification in your controller, set a flag, and then check for the flag in your viewWillDisappear implementation.

NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self       
                  selector:@selector(applicationWillTerminate:)
                      name:UIApplicationWillTerminateNotification
                    object:nil];

Upvotes: 3

probablyCorey
probablyCorey

Reputation: 2498

I haven't used it for your purposes yet, but the UIApplicationWillResignActiveNotification notification might occur before applicationWillTerminate is called.

Just throw...

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResign:) name:UIApplicationWillResignActiveNotification object:NULL];

... into your UIViewController to test it out.

Upvotes: 1

Related Questions