簡體   English   中英

Swift 在呈現新的 ViewController 后關閉當前的 ViewController

[英]Swift dismiss current Viewcontroller after presenting new ViewController

我的場景,我正在嘗試創建多個present ViewController 在這里,在我需要關閉以前的 ViewController 之后呈現新的 ViewController。

ViewController A (RootViewController) next button click to present ViewController C ViewController B然后View Controller B next button click to present ViewController C 現在,如果我關閉 ViewController C 需要顯示ViewController A

例子

這是您可以繼續的方法,

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)
        }
    }
}

由於VCC嵌入在UINavigationController ,因此您需要呈現UINavigationController而不是VCC

對於這個子類UINavigationController並將其設置為storyboardUINavigationController class

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)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM