swift - 警报按钮关闭警报并返回上一个视图控制器
问题描述
我对 Swift 有点陌生,我无法弄清楚这一点。我有一个应该在成功的 URL 请求上显示的警报。用户单击Ok
警报上的按钮后,我需要解除警报,并且需要呈现的控制器在导航堆栈中返回到前一个视图控制器。我没有收到任何错误,但没有任何反应。如果我在 CustomClass 中移动警报的整个代码,那么它工作正常。我假设我没有以正确的方式引用 CustomClass。任何帮助将不胜感激!
struct Alert {
static func CustomAlert(vc: UIViewController, title: String, message: String){
var title = "Title...!"
var message = "Message..."
let myAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
myAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (actin) in
myAlert.dismiss(animated: true, completion: nil)
let vc = CustomClass()
vc.GoBackToPreviousVC()
}))
vc.present(myAlert, animated: true, completion: nil)
}
}
class: CustomClass: UIViewController {
func GoBackToPreviousVC(){
navigationController?popViewController(animated: true)
}
function Download(){
code for URLRequest...
DispatchQueue.main.async {
if (self.response.Status == "200"){
Alert.CustomAlert(vc: self, title: "", message: "")
}
}
}
}
解决方案
看起来每次你调用你的函数 - 你创建新的视图控制器。这就是为什么什么都没有发生。尝试使用闭包,实现警报功能作为 UIViewController 的扩展或将其作为函数输入传递。选择最适合您的需求。
推荐阅读
- java - 使用 C# 中的 jni4net 连接 JVM 时 webapp 出错
- javascript - 大型firebase函数请求似乎导致连接错误
- jdbc - 如何通过将身份验证代码存储在数据库中而不是 ConCurrentHashMap 在 Spring 中配置 OAuth 2.0
- c - For循环创建顺序名称txt文件
- react-native - React Native WebView 不会在 android 上水平滚动
- scala - 如何将文本文件字符串分配给字典值到一个变量中,以及如何通过在 spark scala 中传递键来提取值?
- ruby - Docker Postgres 容器未针对 Rails 应用程序启动
- python - 为 Django ModelForm 中的特定字段添加 Formset
- tensorflow - Keras 没有检测到 GPU,但 tensorflow 是
- amazon-ec2 - aws 自动缩放导致 Laravel 中的自动注销