ios - 什么是 rootViewController 以及何时使用它?

标签 ios iphone appdelegate rootviewcontroller

在我的 iOS 应用程序中集成支付网关时,我使用了 rootViewController属性如下:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

 UINavigationController *controller = [storyboard instantiateViewControllerWithIdentifier:@"navCtrlID"];

 [[UIApplication sharedApplication].keyWindow setRootViewController:controller];

 [self.navigationController presentViewController:controller animated:YES completion:nil];

它完成了这项工作,但是现在我必须多次按下后退按钮才能返回。

为什么会这样?

最佳答案

RootViewController 是应用程序堆栈上的第一个 ViewController。
您应该只在方法上的 AppDelegate 中设置它:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

在您的情况下,由于您从 Storyboard 实例化它,因此将首先显示 ID 为“navCtrlID”的 NavigationController。如果它只有一个 ViewController,当你按下它时它不应该弹出导航堆栈。

如果您在添加此特定 View 之前显示了 ViewController,则不应那样显示它。相反,使用例如:
[self presentViewController: controller animated:YES completion:nil];

关于ios - 什么是 rootViewController 以及何时使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41261893/

相关文章:

ios - SwiftUI Picker 在通过 NavigationLink 显示时崩溃

ios - 我想像在 JIOtv App 中那样创建 collectionView。这是新东西,互联网上没有答案

iphone - NSDictionary 没有响应 objectForKey 和 valueForKey

iphone - 我收到两个与不正确的 Xcode 语法有关的错误

swift - AppDelegate 不显示 Storyboard 按钮

ios - 如何以编程方式在 UITableViewController 的 NavigationBar 内设置 UISearchDisplayBar

ios - 重启cocos2d中的当前场景

iphone - UI 元素可以包含在 .a 库中吗?

ios - 返回 Root View Controller

ios - 在 ViewController 中调用 App delegate 数组