关闭视图永远不会调用完成函数

dismissing a view never calls completion function

当我关闭视图时,它拒绝触发完成功能,我不知道为什么。

这里我希望 print 语句执行,但它从来没有执行。

@IBAction func tapBack(_ sender: Any) {
    self.navigationController?.popViewController(animated: true)
    self.dismiss(animated: true, completion: {
        print("this should print")
    })
}

源代码

https://github.com/omenking/DismissCompletion

我也试过用 DispatchQueue.main.async 包装它,但我没有成功。

在点击时调用函数不是更好吗?示例:

func dismissController() {
  self.navigationController?.popViewController(animated: true)
  self.dismiss(animated: true, completion: nil)
}

然后在您的按钮上:

@IBAction func tapBack(_ sender: Any) {
  dismissController()
  print("this should print")
}

我刚刚下载了您的项目并尝试了它并打印出来了。

首先,我假设既然你是 popping the view controller,你一定有 pushed it into the navigation Stack.

其次,如果您确实这样做了,那么 self.dismiss 方法将永远不会被调用,因为您已经在它之前弹出了视图控制器。

如果你想让 completion 块工作,你应该 present the ViewController 而不是 pushing it.Then,你可以在 completion 中编写代码块,它将执行。