Question

I have a view controller (view A) presenting a modal view (B) when the user pushed a button and the view B has itself a button to present view C. My problem is that if the user exits the application when the view B or C is shown, the same view will appear next time the application is launched. Is there a way to dismiss the views B and C on exit or to show view A when the application starts? Thanks for your help

Was it helpful?

Solution

I assume by close you mean when the application enters the background.

In your app delegate you can via the applicationDidEnterBackground: method dismiss your controller.

Best way would probably be to add an observer in your view controller class:

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

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"appClosing" object:nil];
    [super dealloc];
}

- (void) appClosing
{
    [self dismissModalViewControllerAnimated:YES];
}

And post the notification in your app delegate:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"appClosing" object:nil];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top