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条回答
一纸荒年 Trace。
2楼-- · 2020-02-08 07:55

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()
查看更多
够拽才男人
3楼-- · 2020-02-08 07:57

You can try out this code

func switchRootViewController(rootViewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
    guard let window = UIApplication.shared.keyWindow else { return }
    if animated {
        UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: {
            let oldState: Bool = UIView.areAnimationsEnabled
            UIView.setAnimationsEnabled(false)
            window.rootViewController = rootViewController
            UIView.setAnimationsEnabled(oldState)
        }, completion: { (finished: Bool) -> () in
            if (completion != nil) {
                completion!()
            }
        })
    } else {
        window.rootViewController = rootViewController
    }
}
查看更多
仙女界的扛把子
4楼-- · 2020-02-08 08:01

In order to get the code snippet from the original question to work, I had to make a change to the third line

let navigationController = self.navigationController!

I am using this code in an @IBAction in the view controller that precedes the new root view controller.

Using the original code, I was receiving an error saying that my view controller had no member called window. After looking at the documentation, I could find no property named window. I'm wondering if the original block of code above was intended to be used inside a UINavigationController file.

Here is the block in its entirety.

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let todayViewController: TodaysFrequencyViewController = storyboard.instantiateViewControllerWithIdentifier("todaysFrequency") as! TodaysFrequencyViewController    
let navigationController = self.navigationController!
navigationController.setViewControllers([todayViewControl ler], animated: true)
查看更多
Anthone
5楼-- · 2020-02-08 08:04

Swift 4

 let storyBoard = UIStoryboard(name: "Your_Storyboard_Name", bundle:Bundle.main)
 self.window = UIWindow(frame: UIScreen.main.bounds)
 let yourVc = storyBoard.instantiateViewController(withIdentifier: "YourIdentifier") as? YourViewController
 if let window = window {
  window.rootViewController = yourVc
   }
 self.window?.makeKeyAndVisible()
查看更多
\"骚年 ilove
6楼-- · 2020-02-08 08:06

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
查看更多
霸刀☆藐视天下
7楼-- · 2020-02-08 08:06

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

Yes, it's possible. How you do it depends on the context...

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

There are actually two things that are commonly called the "root view controller":

  • UIWindow has a rootViewController property, which is writeable.

  • UINavigationController has no rootViewController property, but it does have an initializer called -initWithRootViewController:. You can set the nav controller's "root" view controller by setting it's viewControllers property.

It sounds like you're trying to change the window's root view controller, but the code you show only changes the nav controller's viewControllers property. Try setting the window's rootViewController property directly. Understand, however, that if you take that approach then the navigation controller will go away too. If you want to keep the nav controller, on the other hand, go with your current approach.

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.

More information here would be helpful. What do you mean by "cannot get it to work"? What happens, and what do you expect to happen?

查看更多
登录 后发表回答