Skip to content

Instantly share code, notes, and snippets.

@oaleeapp
Created January 21, 2016 09:24
Show Gist options
  • Save oaleeapp/31142787cc52a76df9d5 to your computer and use it in GitHub Desktop.
Save oaleeapp/31142787cc52a76df9d5 to your computer and use it in GitHub Desktop.
UIViewController extension for creating UIAlertController
func newDetail(sender: UIBarButtonItem) {
let doneAction = UIAlertAction(title: "Done", style: .Default) { (action) -> Void in
// Do something
}
let firstTextFieldConfigure = { (textfield : UITextField) in
textfield.placeholder = "Please enter the class of detail"
}
let secondTextFieldConfigure = {(textfield : UITextField) in
textfield.placeholder = "Please enter the content of detail"
}
let controller = alertController("New Detail", message: nil, preferredStyle: .Alert, actions: [doneAction], textFieldConfigures: [firstTextFieldConfigure, secondTextFieldConfigure])
presentViewController(controller, animated: true, completion: nil)
}
import UIKit
typealias textFieldConfigurationClosure = (UITextField) -> Void
extension UIViewController {
func alertController(title : String?, message : String?, preferredStyle : UIAlertControllerStyle, actions: [UIAlertAction], textFieldConfigures: [textFieldConfigurationClosure]) -> UIAlertController {
let alertController = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
for action in actions {
alertController.addAction(action)
}
for configureClosure in textFieldConfigures {
alertController.addTextFieldWithConfigurationHandler(configureClosure)
}
return alertController
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment