【问题标题】:Alert button to dismiss Alert and go back to previous View Controller警报按钮关闭警报并返回上一个视图控制器
【发布时间】:2023-03-23 08:21:01
【问题描述】:

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

      }

    }
}

【问题讨论】:

    标签: swift uialertcontroller


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      不要创建新实例let vc = 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)
      
              if let controller = vc as? CustomClass {
                 controller.GoBackToPreviousVC()
              }
             }))
              vc.present(myAlert, animated: true, completion: nil)
         }
      }
      

      最好使用协议而不是硬代码类

      protocol Goback {
          func GoBackToPreviousVC()
      }
      
      
      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)
      
              if let controller = vc as? Goback {
                 controller.GoBackToPreviousVC()
              }
             }))
              vc.present(myAlert, animated: true, completion: nil)
         }
      }
      

      并使用您要在其中使用警报的协议确认您的班级

      class CustomClass: UIViewController,Goback {
      
          func GoBackToPreviousVC(){
      
              navigationController?.popViewController(animated: true)
          }
      }
      

      【讨论】:

        【解决方案3】:

        将警报修改为在UIViewController 的扩展内,并在导航堆栈中使用self 关闭和popViewController,代码如下:

        extension UIViewController {
            func CustomAlert(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)
                    self.navigationController?.popViewController(animated: true)
                }))
                 present(myAlert, animated: true, completion: nil)
            }
         }
        

        用法:

        class: CustomClass: UIViewController {
        
            function Download(){
        
              // code for URLRequest...
        
              DispatchQueue.main.async {
                if (self.response.Status == "200") {
                    self.CustomAlert(title: "", message: "")
                }
              }
            }
        }
        

        【讨论】:

          【解决方案4】:

          斯威夫特 5.3

          您添加实用程序类。我就是这样用的。

          public class UtilsClass: NSObject  {
              public static let shared = UtilsClass()
          
              public func customAlertYN(ViewController:UIViewController,title:String!,actionTitle:String!,message:String!,okButtonString:String!,cancelButtonString:String!,okCallback: @escaping () -> Void = {}){
                      let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
                      let ok = UIAlertAction(title: okButtonString, style: .default, handler: { (action) -> Void in
                          okCallback()
                      })
          
                      let cancel = UIAlertAction(title: cancelButtonString, style: .cancel) { (action) -> Void in
          
                      }
          
                      alert.addAction(ok)
                      alert.addAction(cancel)
                      ViewController.present(alert, animated: true, completion: nil)
                  }
                }
          

          并使用这种方式。添加您的视图控制器。

          UtilsClass.shared.customAlertYN(ViewController: self, title: "Alert", actionTitle: "", message: "Are you sure you want to logout?", okButtonString: "Yes", cancelButtonString: "No",okCallback: {
                          //TODO: 
             })
          

          【讨论】:

            猜你喜欢
            • 2020-10-02
            • 2016-03-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多