I have the following code repeated in many of my view controllers
. I would like to refactor it into a global class/view controller. However, I still want it to be s
Swift 4.0
You can use action closure for multiple actions.
extension UIViewController {
func popupAlert(title: String?, message: String?, actionTitles:[String?], actions:[((UIAlertAction) -> Void)?]) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
for (index, title) in actionTitles.enumerated() {
let action = UIAlertAction(title: title, style: .default, handler: actions[index])
alert.addAction(action)
}
self.present(alert, animated: true, completion: nil)
}
}
And you can use like this in UIViewController
popupAlert(title: kTitle, message: "This is test alert!!!!" , actionTitles: ["Ok","Cancel"], actions: [ { action1 in
//perform action for OK button
}, { action2 in
//perform action for cancel button
}])