Created
January 21, 2016 09:24
-
-
Save oaleeapp/31142787cc52a76df9d5 to your computer and use it in GitHub Desktop.
UIViewController extension for creating UIAlertController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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