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.
The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.
The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller's view as the content view of the window.
Swift 4.2
May be you should try this
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil) let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "respectiveIdentifier") as! ViewController let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.window?.rootViewController = redViewController
Swift 4, 5, 5.1
let story = UIStoryboard(name: "Main", bundle:nil) let vc = story.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController UIApplication.shared.windows.first?.rootViewController = vc UIApplication.shared.windows.first?.makeKeyAndVisible()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With