我有一个 View Controller ( View A),当用户按下按钮时呈现模态视图(B),并且 View B 本身有一个按钮来呈现 View C。我的问题是,如果用户退出应用程序时显示 View B 或 C 时,下次启动应用程序时将出现相同的 View 。有没有办法在退出时关闭 View B 和 C 或在应用程序启动时显示 View A? 感谢您的帮助
最佳答案
我假设关闭是指应用程序进入后台时。
在您的应用程序委托(delegate)中,您可以通过 applicationDidEnterBackground: 方法关闭您的 Controller 。
最好的方法可能是在 View Controller 类中添加观察者:
- (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];
}
并在您的应用程序委托(delegate)中发布通知:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"appClosing" object:nil];
}
关于objective-c - 应用程序退出时关闭模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3490655/