警报按钮关闭警报并返回到上一个视图控制器

Alert button to dismiss Alert and go back to previous View Controller

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

      }

    }
}

不要创建新实例 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)
   }
}

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

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 与您要在其中使用 Alert

的协议
class CustomClass: UIViewController,Goback {

    func GoBackToPreviousVC(){

        navigationController?.popViewController(animated: true)
    }
}

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

将警报修改为 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: "")
        }
      }
    }
}

Swift 5.3

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

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)
        }
      }

并使用这种方式。添加您的 viewController.

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