简体   繁体   中英

Swift dismiss current Viewcontroller after presenting new ViewController

My scenario, I am trying to create multiple present ViewController . Here, presenting new ViewController after I need to dismiss previous ViewController.

ViewController A (RootViewController) next button click to presenting ViewController B then View Controller B next button click to present ViewController C . Now, If I close ViewController C need to show ViewController A .

例子

Here is how you can proceed,

class VCA: UIViewController {
    @IBAction func onTapNextButton(_ sender: UIButton) {
        if let controller = self.storyboard?.instantiateViewController(withIdentifier: "VCB") as? VCB {
            self.present(controller, animated: true, completion: nil)
        }
    }
}

Since VCC is embedded in a UINavigationController , you need to present UINavigationController instead of VCC .

For this subclass UINavigationController and set it as class of UINavigationController in the storyboard .

class VCB: UIViewController {
    @IBAction func onTapNextButton(_ sender: UIButton) {
        if let controller = self.storyboard?.instantiateViewController(withIdentifier: "NavVC") {
            self.dismiss(animated: false, completion: nil)
            self.presentingViewController?.present(controller, animated: true, completion: nil)
        }
    }
}

class NavVC: UINavigationController {}

class VCC: UIViewController {
    @IBAction func onTapCloseButton(_ sender: UIButton) {
        self.navigationController?.dismiss(animated: true, completion: nil)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM