我们如何更改navigationController的rootview?

时间:2018-03-16 01:44:36

标签: ios swift3 uinavigationcontroller viewcontroller

最初我的应用程序以我在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,我真的很感激。

感谢。 :)

1 个答案:

答案 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()