views:

483

answers:

2

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!

+1  A: 

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.

probablyCorey
UIApplicationWillResignActiveNotification doesn't seem to be getting triggered when the application is closed due to the menu button. applicationWillTerminate always fires, though.
Alex
+3  A: 

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];
Nathan de Vries
I'm seeing that viewWillDisappear gets called before that notification. So how will setting flags matter?
dizy