【问题标题】:Present Next ViewController to Dismiss current ViewController Using Swift使用 Swift 呈现下一个 ViewController 以关闭当前的 ViewController
【发布时间】:2019-06-24 17:53:32
【问题描述】:

我的场景,我有三个ViewControllers,那些ViewControllers 第一个、第二个、第三个。在这里,首先ViewControllerUIButton点击展示模型第二个ViewController然后第二个ViewControllerUIButton点击展示模型第三个ViewController。我需要实现第二个ViewController 按钮单击以在呈现第三个ViewController 后关闭当前ViewController,因为一旦我关闭第三个ViewController,我需要显示第一个ViewController。如何做到这一点?

注意:我正在为第三个 ViewController 使用 NavigationController,我也在使用 Storyboard

这是我的代码:

@IBAction func ClickThirdVC(_ sender: Any) {
    if let VC_Third = self.storyboard?.instantiateViewController(withIdentifier: "thirdviewcontroller") as? ThirdvViewController {
        weak var pvc = self.presentingViewController
        VC_B.modalTransitionStyle = .crossDissolve
        let navController = UINavigationController(rootViewController: VC_B)
        self.dismiss(animated: true, completion: {
            pvc?.present(navController, animated: true, completion: nil)
        })
    }
}

【问题讨论】:

    标签: swift uinavigationcontroller


    【解决方案1】:

    您可以在呈现第三个UIViewController 后尝试关闭self,在当前函数的完成处理程序中。

    secondVC?.present(navController, animated: true) {
                self.dismiss(animated: false, completion: nil) //dismiss self after presenting the third. 
            }
    

    【讨论】:

    • 以上代码我需要放在哪里?在第二个或这个视图控制器内? @Mohammad S
    • @JamKu 在您展示第三个 VC 的第二个 VC 中
    • 根据您的解决方案,我遇到了崩溃
    • 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图在其自身上呈现模态视图控制器。呈现控制器是 。' @Mohammad S
    • 你叫它两次吗?还是你替换了代码?
    猜你喜欢
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2020-04-27
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    相关资源
    最近更新 更多