Dismiss modal view controller on application exit

会有一股神秘感。 提交于 2020-01-16 00:52:05

问题


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:


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];
}


来源:https://stackoverflow.com/questions/3490655/dismiss-modal-view-controller-on-application-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!