首页 > 解决方案 > 警报按钮关闭警报并返回上一个视图控制器

问题描述

我对 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: "")
        }

      }

    }
}

标签: swiftuialertcontroller

解决方案


看起来每次你调用你的函数 - 你创建新的视图控制器。这就是为什么什么都没有发生。尝试使用闭包,实现警报功能作为 UIViewController 的扩展或将其作为函数输入传递。选择最适合您的需求。


推荐阅读