views:

28

answers:

1

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

+1  A: 

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];
}
vakio
Thanks for your answer. I tried your solution and it works. I also found another simpler solution, displaying the main view on exit (Also I get a warning when I compile) :- (void)applicationDidEnterBackground:(UIApplication *)application { [self presentModalViewController:viewController.view animated:NO];}Cheers
Cyril