我正在做一个 Objective-C 应用程序,它有几个独立的 ViewControllers。 当用户按下主页按钮并且应用程序进入后台模式时,我想显示我的主 ViewController。 我找到的所有解决方案都是针对导航 Controller 的。 在这种情况下,如何启动(以及在何处)ViewController?
谢谢
已解决: 最后我通过添加 AppDelegate 解决了这个问题:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
ViewController *viewController=[[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
self.window.rootViewController = viewController;
}
最佳答案
您应该在主视图 Controller 中注册 UIApplicationDidEnterBackgroundNotification
。发布通知后,您应该将导航堆栈弹出到主视图 Controller 。
[self.navigationController popToRootViewControllerAnimated:YES];
或者,如果这不是您应该使用的 Root View Controller :
[self.navigationController popToViewController:self animated:YES];
关于ios - 按下主页按钮时启动主 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35015878/