如何在 UIAlertController 中点击 "Ok" 操作时关闭呈现视图控制器
How to Dismiss Presenting View Controller when "Ok" Action Tapped in UIAlertController
我试图在 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)
}
但是,在单击警报中的“确定”后,会打印“确定已选择”,但不会关闭视图控制器。调试器中没有显示任何其他内容。
尝试在主线程上关闭它,并检查 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)
}
要检查是否显示 ViewController,请使用 self.isBeingPresented
属性。
我试图在 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)
}
但是,在单击警报中的“确定”后,会打印“确定已选择”,但不会关闭视图控制器。调试器中没有显示任何其他内容。
尝试在主线程上关闭它,并检查 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)
}
要检查是否显示 ViewController,请使用 self.isBeingPresented
属性。