【问题标题】:Swift dismiss current Viewcontroller after presenting new ViewController在呈现新的 ViewController 后 Swift 关闭当前的 ViewController
【发布时间】:2019-11-15 04:44:25
【问题描述】:

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

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

【问题讨论】:

  • @Craz1k0ek 也没有什么明确的想法我也在寻找现在的模型视图控制器
  • 我不明白你的问题。当您解雇 C 时,您想显示 A 吗?或者您想在展示下一个 VC 之前关闭当前的 VC?
  • @TejaNandamuri 当我解雇 C 时,我需要显示 A。
  • 最好使用导航控制器,并从中推送和弹出视图控制器,而不是直接显示和关闭。

标签: ios swift


【解决方案1】:

这里是你可以继续的方法,

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,并在storyboard 中将其设置为UINavigationControllerclass

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

【讨论】:

  • 我在 VC-C 中有导航控制器。如何添加?
  • 你的意思是VC-C是navigationController的rootViewController?
  • 是的,我嵌入了导航控制器。
  • 更新了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
相关资源
最近更新 更多