我试图解散一个ViewController在一个IBAction调用遣散ViewController

  @IBAction func cancel(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("cancel")
}

@IBAction func done(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("done")
}

我可以在控制台输出中看到println消息,但ViewController从未被解散。有什么问题吗?


当前回答

如果你在父VC中使用当前方法,那么你应该调用这个函数,来解散子VC

self.dismiss(animated: true, completion: nil)

如果你用push方法调用子VC,用这个方法来解散子VC

self.navigationController?.popViewController(animated: true)

其他回答

@IBAction func back(_ sender: Any) {
        self.dismiss(animated: false, completion: nil)
    }

Use:

self.dismiss(animated: true, completion: nil)

而不是:

self.navigationController.dismissViewControllerAnimated(true, completion: nil)

在Swift 3.0到4.0中,只需在函数中输入以下内容即可:

self.dismiss(animated: true, completion: nil)

或者如果你在导航控制器中,你可以“弹出”它:

self.navigationController?.popViewController(animated: true)

将你想要解散的视图嵌入到一个NavigationController中 添加一个BarButton与“完成”作为标识符 使用选定的Done按钮调用助理编辑器 为这个按钮创建一个IBAction 将这一行添加到括号中: 自我。disdisviewcontrolleranimated (true, completion: nil)

不要创建任何从取消或完成到其他VC的segue,只把这段代码写在你的按钮@IBAction上

@IBAction func cancel(sender: AnyObject) {
    dismiss(animated: false, completion: nil)
}