Q:

Common function for alert view?

I want to show alertview in my controller classes. So i have created a common function to show alert & respond to it's action buttons.
In Commonfunctions.swift 
i have created one function like below 
 func showActionAlertView(title:String,message:String,vc:UIViewController) -> Void {

let Alert = UIAlertController(title: "Warning", message:  NSLocalizedString("Alert_Delete", comment: ""), preferredStyle: UIAlertControllerStyle.alert)

Alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
  Constant.commonfunction.showLoader(withMessage: "Loading")

}))

Alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
  print("Handle Cancel Logic here")
}))

vc.present(Alert, animated: true, completion: nil)
}

and created a protocol in Commonfunctions.swift.
protocol alertDelegate
{
  func okAction(controller:UIViewController)
  func cancelAction(controller:UIViewController)
}

In controller class i have added this 
class 
MyController:UIViewController,UITableViewDelegate,UITableViewDataSource,CLLocationManagerDelegate,alertDelegate
{
  var delegate:alertDelegate! = nil
}

call back functions goes here
 func okAction(controller: UIViewController) {

    print("Ok Action")

  }
  func cancelAction(controller: UIViewController) {

    print("Cncel Action")

  }

and i am showing the alert below like this 
  Constant.commonfunction.showActionAlertView(title: NSLocalizedString("Success", comment: ""), message: NSLocalizedString("CreateProperty_Alert_created", comment: ""), vc: self)

I am not able to call okAction & cancelAction methods. Tell how to implement call back.

A:

Yo need to create an object of that delegate like this 
var delegate: alertDelegate?

and with delegate you can call this functions like this 
also you need to assign the delegate as self while calling this method showActionAlertView
like this delegate = vc
Alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
  Constant.commonfunction.showLoader(withMessage: "Loading")
      delegate?.okAction(controller: vc)
}))

Alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
  print("Handle Cancel Logic here")
  // Call delegate for Cancel Action Here
    delegate?.cancelAction(controller: vc)
}))

Edited
Do it like this 
class Utility : NSObject {

var delegate: alertDelegate?

func showActionAlertView(title:String,message:String,vc:UIViewController) -> Void {
    let Alert = UIAlertController(title: "Warning", message:  NSLocalizedString("Alert_Delete", comment: ""), preferredStyle: UIAlertControllerStyle.alert)

    Alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
        self.delegate?.okAction(controller: vc)

    }))

    Alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
        print("Handle Cancel Logic here")
        self.delegate?.cancelAction(controller: vc)
    }))

    vc.present(Alert, animated: true, completion: nil)
}
}

And use it like this 
func showAlert() {
    let vcUtility = Utility()
    vcUtility.delegate = self
    vcUtility.showActionAlertView(title: "Message", message: "Message", vc: self)

}

func okAction(controller: UIViewController) {
    print("Ok")
}
func cancelAction(controller: UIViewController) {
    print("Cancel")
}

