UIAlertController Swift extension with handler inside initialization 🔶
Why? Because UIAlertController has handlers inside each action, forcing the developers to have duplicated code. Instead, we can have a switch inside a global handler that is called when any action inside the UIAlertController is called
Drag the LFAlertController.swift from /Sources to your project
Don't forget to add use_frameworks! at the top of your Podfile
pod 'LFAlertController'
.Package(url: "https://github.com/awesome-labs/LFAlertController.git", majorVersion: 0)
let action1 = UIAlertAction(title: "Second Action", style: .Default)
UIAlertController with Handler
self.controller = UIAlertController(title: "hey", message: "ho", preferredStyle: .Alert,actions:[action0,action1])
{ (action,index) -> () in
//Printing index of Action
print("\(index) - \(action.title!)")
}
self.controller.show()
UIAlertAction with handler
let action0 = UIAlertAction(title: "First Action", style: .Default, handler: nil)
UIAlertController without Handler
self.controller = UIAlertController(title: "hey", message: "ho", preferredStyle: .Alert, actions: [action0,action1])