问题
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