首页 > 解决方案 > 呈现新的 ViewController 后 Swift 关闭当前的 ViewController

问题描述

我的场景,我正在尝试创建多个present ViewController. 在这里,在我需要关闭以前的 ViewController 之后呈现新的 ViewController。

ViewController A (RootViewController)下一步按钮单击以呈现,ViewController B然后查看控制器 B 下一步按钮单击以呈现ViewController C。现在,如果我关闭 ViewController C 需要显示ViewController A.

例子

标签: iosswift

解决方案


这是您可以继续的方法,

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嵌入在 a 中UINavigationController,因此您需要呈现UINavigationController而不是VCC.

对于这个子类并将UINavigationController其设置classUINavigationController.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)
    }
}

推荐阅读