ios - 按下主页按钮时启动主 ViewController

标签 ios objective-c

我正在做一个 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/

相关文章:

ios - 多个 View Controller 上的相同 UIProgressView

ios - 以编程方式添加约束时应用程序崩溃

ios - 我在哪里/什么时候可以向我的 `UIWindow` 中的 `UIViewController` 添加内容?

ios - 导航栏标题仅更改一次(不再变白然后返回)

ios - 用于 iPhone 语音识别的 Shazam

ios - 使用变量更改 UIButton 图像

ios - 仅在键盘上显示字母(禁用数字和标点符号)

objective-c - 如何在没有现有类的情况下声明用于 NSMenuItem 的方法?

iphone - 强制调用 UIViewController 子类方法

ios - TableView 标题部分不透明