最初我的应用程序以我在AppDelegate文件中设置的视图控制器(TabBarView)开始。
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let tabBarView = TabBarView()
navigationController = UINavigationController(rootViewController: tabBarView)
window?.rootViewController = navigationController
稍后在应用中,当满足几个条件时,我想将我的rootController更改为新的(MusicPage)。
目前我已经创建了一个新的NavigationController对象来显示我的(MusicPage)viewController但是我认为我的(TabBarView)viewController可能在后台运行并消耗内存。
如果有人告诉我如何更改我的初始rootViewController,我真的很感激。
感谢。 :)
答案 0 :(得分:1)
答案可以在
找到Set rootViewController of UINavigationController by method other than initWithRootViewController
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav
编辑:SWIFT 4
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let yourVc = UIViewController() // your view controller
appDelegate.window?.rootViewController = yourVc
appDelegate.window?.makeKeyAndVisible()