Swift ios set a new root view controller

2020-02-08 07:32发布

I wonder if its possible to set a new root VC?

My app gets init with a uinavigation controller that has a table view to be the root VC.

Then from the table view I am running another segue to a login window (present modally) If you then login you end up in the red VC/account page. What I want to do now is to set the red VC to be the new root VC of the app, and remove all underlying VC's. So that I can show a menu button/icon instead of a "Back" button

I have found this but I dont understand how to use it:

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let yourViewController: ViewController = storyboard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as! ViewController

        let navigationController = self.window?.rootViewController as! UINavigationController
        navigationController.setViewControllers([yourViewController], animated: true)

But I cannot get it to work. So is it possible to make the red vc in the picture act as the new root VC.

enter image description here

18条回答
仙女界的扛把子
2楼-- · 2020-02-08 08:06

For swift 4.0.

In your AppDelegate.swift file in didfinishedlaunchingWithOptions method, put the following code.

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let rootVC = MainViewController() // your custom viewController. You can instantiate using nib too. UIViewController(nib name, bundle)

    let navController = UINavigationController(rootViewController: rootVC) // Integrate navigation controller programmatically if you want
    window?.rootViewController = navController

    return true
}

Hope it will work just fine.

查看更多
我命由我不由天
3楼-- · 2020-02-08 08:07

Swift 3 Update:-

let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "testController") as! TestController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = testController
查看更多
做个烂人
4楼-- · 2020-02-08 08:11

UINavigationController has a viewControllers property, which is a NSArray, And you can replaced it with your own NSArray of view controllrs.

This can be done as show in below sample code.

let newViewController = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewControllerollerID") as! YourViewController

    let customViewControllersArray : NSArray = [newViewController]
    self.navigationController?.viewControllers = customViewControllersArray as! [UIViewController]

And if you want to show this new root view controller you can just call UINavigationController's popToRootViewController() method.

self.navigationController?.popToRootViewControllerAnimated(true)
查看更多
beautiful°
5楼-- · 2020-02-08 08:13

You can use this bit of code:

let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

let customViewControllersArray : NSArray = [newViewController]
self.navigationController?.viewControllers = customViewControllersArray as! [UIViewController]
self.navigationController?.pushViewController(newViewController, animated: true)
查看更多
祖国的老花朵
6楼-- · 2020-02-08 08:13

Swift 3 AppDelegate file:::

@IBAction func btnGoBack(_ sender: UIButton){

   self.goToHomeVC()
}

func goToHomeVC(){

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let viewController = storyboard.instantiateViewController(withIdentifier :"HomeVC") as! HomeVC

    let navController = UINavigationController.init(rootViewController: viewController)

    if let window = self.appDelegate.window, let rootViewController = window.rootViewController {

        var currentController = rootViewController

        while let presentedController = currentController.presentedViewController {

            currentController = presentedController
        }

        currentController.present(navController, animated: true, completion: nil)
    }
}
查看更多
贪生不怕死
7楼-- · 2020-02-08 08:15

once you are in the vc that you want to set as root, just add in your viewDidLoad:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = self

*As per best practice you should check if it is already the root, if not execute the code above.

查看更多
登录 后发表回答