【问题标题】:When should I set rootViewController? it's very confused什么时候应该设置 rootViewController?很困惑
【发布时间】:2020-12-22 19:10:00
【问题描述】:

您好,我正在为 rootViewController 苦苦挣扎,我留下了一些代码片段,下面有解释,请告诉我。

如果我像下面那样做,它可以工作并且一切都很好。

    private func presentLogin() {
        log.info("presenting login..")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let vc = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
        vc.modalPresentationStyle = .fullScreen
        appDelegate.window!.rootViewController = vc
        present(vc, animated: false)
    }

然后,如果我执行下面的代码,ios 只显示白色空白页..

    private func presentMain() {
        log.info("presenting main..")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! MainTabBarController
        vc.modalPresentationStyle = .fullScreen
        appDelegate.window!.rootViewController = vc
        present(vc, animated: false)
    }

但是当我删除代码时

appDelegate.window!.rootViewController = vc

一切都很好。

换句话说,下面的代码只在第一次工作。

appDelegate.window!.rootViewController = vc

为什么?我错过了什么?,我不明白..

【问题讨论】:

    标签: ios window rootviewcontroller


    【解决方案1】:

    好像有很多bugs关于根视图控制器的交换。切换根视图控制器也是一种不常见的“导航”方式。我会推荐一种不同的方法:

    • 应用启动时,使用“启动”视图控制器。这是您的根视图控制器,它始终是您的根视图控制器
    • 如果需要登录,则显示登录视图控制器
      • 成功登录后,关闭登录视图并继续
    • 如果已登录,则显示主应用程序的入口视图

    【讨论】:

      猜你喜欢
      • 2021-09-16
      • 2010-10-10
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多