1

I have a helper to show my alerts

import UIKit

class AlertDialog {
    class func showAlert(_ title: String, message: String, viewController: UIViewController) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(OKAction)
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
        alertController.addAction(cancelAction)
        viewController.present(alertController, animated: true, completion: nil)
    }
}

How can I manage the actions in my View Controllers?

I am calling the func like this;

AlertDialog.showAlert("Ok", message: "Some Message", viewController: self)

I need to get the handler option. What do I change the "handler: nil" to?

Enrique Bermúdez
  • 1,740
  • 2
  • 11
  • 25
David Henry
  • 1,972
  • 20
  • 43
  • 1
    Possible duplicate of [Writing handler for UIAlertAction](https://stackoverflow.com/questions/24190277/writing-handler-for-uialertaction) – heyfrank Mar 03 '18 at 17:26
  • the proposed duplicate is using the AlertAction within the View Controller as opposed to having a separate helper class. – David Henry Mar 03 '18 at 17:44

2 Answers2

1

You should be able to do this:

class func showAlert(_ title: String, message: String, viewController: UIViewController, ok: ((UIAlertAction) -> Void)?, cancel: ((UIAlertAction) -> Void)?) {
    let okAction = UIAlertAction(title: "Ok", style: .default, handler: ok)
    let cancelAction = UIAlertAction(title: "Ok", style: .default, handler: cancel)
}

And then you would use this as follows:

AlertDialog.showAlert("Ok", message: "Some Message", viewController: self, ok: { (alertAction) in 
    // do something for ok
}, cancel: { (alertAction) in
    // do something for cancel
})
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
1

You can add two handlers parameters to your showAlert method, one for the ok action and another for the cancel action. So your code might look something like this:

class AlertDialog {
    class func showAlert(_ title: String, message: String, viewController: UIViewController,
                     okHandler: (() -> Swift.Void),
                     cancelHandler: (() -> Swift.Void)) {

        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "OK", style: .default, handler: okHandler)
        alertController.addAction(OKAction)
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: cancelHandler)
        alertController.addAction(cancelAction)
        viewController.present(alertController, animated: true, completion: nil)
    }
}

From your viewController you will call:

AlertDialog.showAlert("Ok", message: "Some Message", viewController: self, okHandler: {
            //OK Action
        },cancelAction: {
            //Cancel Action
        })
Enrique Bermúdez
  • 1,740
  • 2
  • 11
  • 25
  • 2
    Given the question I think answer would benefit from showing an example of how the new parameters could be used. – rmaddy Mar 03 '18 at 17:32