问题描述
您好,我在使用 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
为什么?我错过了什么?,我不明白..
解决方法
似乎有很多关于根视图控制器交换的bugs。切换根视图控制器也是一种不常见的“导航”方式。我会推荐一种不同的方法:
- 在应用启动时,使用“启动”视图控制器。这是你的根视图控制器,它一直是你的根视图控制器
- 如果需要登录,请提供登录视图控制器
- 成功登录后,关闭登录视图并继续
- 如果已登录,则显示主应用程序的入口视图