【问题标题】:How to Dismiss Presenting View Controller when "Ok" Action Tapped in UIAlertController如何在 UIAlertController 中点击“确定”操作时关闭呈现视图控制器
【发布时间】:2020-10-16 14:31:04
【问题描述】:

我正在尝试在 UIAlertAction 的完成处理程序中关闭当前视图控制器,但它没有关闭。我编写了以下代码(加载指示器只是一个加载警报控制器,当数据成功上传时我将其关闭):

loadingIndicator.dismiss(animated: true) {                           
      let success = UIAlertController(title: "Successfully Uploaded", message: "", preferredStyle: .alert)
      let ok = UIAlertAction(title: "Ok", style: .default, handler: { _ in
               print("Ok selected") //this is working correctly
               self.dismiss(animated: true, completion: nil) //this is not
      })
                      
      success.addAction(ok)
      self.present(success, animated: true, completion: nil)
}

但是,在警报中单击“Ok”后,会打印“Ok selected”,但不会关闭视图控制器。调试器中没有其他任何显示。

【问题讨论】:

  • 尝试关闭主线程上的警报。
  • 我正在从另一个函数调用此代码,因为它是由按钮调用的。我也尝试在DispatchQueue.main.async 调用中包含dismiss,但这也没有用。
  • 检查VC是否出现?
  • 我该怎么做?
  • 更新了答案,我确定这与您的导航层次结构有关。

标签: ios swift uialertcontroller dismiss uialertaction


【解决方案1】:

尝试在主线程上关闭它,并检查 ViewController 是否在导航层次结构中呈现或推送。

loadingIndicator.dismiss(animated: true) {
    let success = UIAlertController(title: "Successfully Uploaded", message: "", preferredStyle: .alert)
    let ok = UIAlertAction(title: "Ok", style: .default, handler: { _ in
        DispatchQueue.main.async {
            self.dismiss(animated: true, completion: nil)
             // If pushed use PopViewController on navigation
             // self.navigationController?.popViewController(animated: true)
        }
    })
    success.addAction(ok)
    self.present(success, animated: true, completion: nil)
}

使用self.isBeingPresented 属性检查是否显示了 ViewController。

【讨论】:

    猜你喜欢
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多