Can I preserve a modally presented view controller when setting the root view controller?

谁都会走 提交于 2021-01-29 08:33:05

问题


Let's say I have three view controllers. On the first one, I tap a button and present the second view controller modally. On the second one, I tap a button and store the presenting view controller (first), then set the third view controller as the root view controller. Finally, I have a button on the third view controller that grabs the stored controller and sets that as the root view controller.

When I restore the first view controller, I find that its presentedViewController property is nil. I wonder if I can preserve the navigation stack and have the modal presented when I restore the first view controller?

In this example I'm storing the view controller inside AppDelegate for simplicity.

Code:

FirstViewController.swift

    @IBAction func presentViewController(_ sender: Any) {
        let storyboard = UIStoryboard(name: "SecondVC", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "Second")
        present(controller, animated: true, completion: nil)
    }
SecondViewController.swift

    @IBAction func secondButtonCLick(_ sender: Any) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.savedVC = presentingViewController

        let storyboard = UIStoryboard(name: "Third", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "ThirdVC")

        UIApplication.shared.keyWindow?.rootViewController = controller
    }
ThirdViewController.swift

    @IBAction func thirdBtnTouch(_ sender: Any) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let rootVC = appDelegate.savedVC
        UIApplication.shared.keyWindow?.rootViewController = rootVC
    }

来源:https://stackoverflow.com/questions/59956631/can-i-preserve-a-modally-presented-view-controller-when-setting-the-root-view-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!