ios - 在 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)
}
但是,在警报中单击“Ok”后,会打印“Ok selected”,但不会关闭视图控制器。调试器中没有其他任何显示。
解决方案
尝试在主线程上关闭它,并检查 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
属性。
推荐阅读
- python - 无法使用 Cartopy 绘制散点图,这在底图中很好地显示
- sql - BigQuery 外部联接
- django - Django:奇怪的行为:每当传递一个 datetime 对象时,它就会被存储为元组。并非总是发生
- swift - 在 NSMutableArray 中搜索
- asp.net-core - 如何在 ASP.Net 核心集成测试中控制日志级别
- xgboost - 如何在 jupyter 中隐藏来自 xgboost 库的警告?
- django - Django 管理员在添加表单中预编译当前登录用户的字段
- c++ - 如何破坏单例实例,或者为什么下面的代码适用于析构函数?
- datatable - 提取所有变量名并将其重命名为另一种人类语言的有效方法是什么。有没有好的脚本或扩展?
- twitter-bootstrap - 如何在引导选择国家选择器控件中获取国家的全名