在应用程序退出时关闭模式视图控制器 [英] Dismiss modal view controller on application exit
问题描述
当用户按下按钮时,我有一个视图控制器(视图A)呈现一个模式视图(B),视图B本身具有一个按钮来呈现视图C.我的问题是,如果用户在显示了视图B或C,下次启动该应用程序时将出现相同的视图.有没有办法在退出时关闭视图B和C或在应用程序启动时显示视图A? 谢谢您的帮助
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
推荐答案
我假设您的意思是当应用程序进入后台时.
I assume by close you mean when the application enters the background.
在您的应用程序委托中,您可以通过applicationDidEnterBackground:方法关闭您的控制器.
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];
}
这篇关于在应用程序退出时关闭模式视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!