Ialertcontroller also replaces Uialertview and Uiactionsheet, unifying the concept of alert at the system level-either in modal or PopOver mode.
Uialertcontroller is a subclass of Uiviewcontroller, not its previous way. So the new alert can benefit a lot from the view controller's presentation of the relevant configuration.
Uialertcontroller is initialized with the title and message parameters, whether it is to be displayed in alert or action sheet mode. Alert appears in modal form in the currently displayed view Controller center, and action sheet slides out at the bottom. Alert can have both a button and an input box, and action sheet only supports buttons.
The new approach does not put all the alert button configurations in the initialization function, but introduces a new class Uialertaction object that can be configured after initialization. This form of API refactoring allows for greater control over the number, type, and order of buttons. It also discards the delegate used by Uialertview and Uiactionsheet, but uses a simpler callback when it's done.
Comparison of new and old Alert methods
Standard Alert Style
Old method: Uialertview
| 123 |
let alertview = uialertview (title: " Default style " , MESSAGE:  " a Standard alert. " , DELEGATE: SELF, CANCELBUTTONTITLE:  "Cancel" , OTHERBUTTONTITLES:  "OK" ) alertview.alertviewstyle = . Default alertview.show () |
| 123456 |
// MARK: UIAlertViewDelegatefunc alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { switchbuttonIndex { // ... }} |
New method: Uialertcontroller
| 123456789101112 |
let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .Alert)let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in // ...}alertController.addAction(cancelAction)let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in // ...}alertController.addAction(OKAction)self.presentViewController(alertController, animated: true) { // ...} |
Standard Action Sheet Style
Uiactionsheet
| 123 |
let actionsheet = uiactionsheet (title: " Takes the appearance of the bottom bar if specified; otherwise, same as uiactionsheetstyledefault. " , DELEGATE: SELF, CANCELBUTTONTITLE:  "Cancel" , DESTRUCTIVEBUTTONTITLE:  "Destroy" , OTHERBUTTONTITLES:  "OK" ) actionsheet.actionsheetstyle = . Default actionsheet.showinview (self.view) |
| 123456 |
// MARK: UIActionSheetDelegatefunc actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { switchbuttonIndex { ... }} |
Uialertcontroller
| 12345678910111213141516 |
let alertController = UIAlertController(title: nil, message: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", preferredStyle: .ActionSheet)let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in // ...}alertController.addAction(cancelAction)let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in // ...}alertController.addAction(OKAction)let destroyAction = UIAlertAction(title: "Destroy", style: .Destructive) { (action) in println(action)}alertController.addAction(destroyAction)self.presentViewController(alertController, animated: true) { // ...} |
New features
Uialertcontroller does not just clean up the existing API, it does a standardized generalization. Previously, the preset style was a lot more idle (swizzling, although it could provide more functionality but there was a great deal of risk). Uialertcontroller makes it possible for things that looked magical before.
Uialertcontroller is not just a cleanup of pre-existing APIs, it's a generalization of them. Previously, one is constrained to whatever presets were provided (swizzling in additional functionality at their own risk ). With Uialertcontroller, it's possible to does a lot more out-of-the-box:
Alert with alert button
This behavior has been covered by Uialertactionstyle, with a total of three types:
. Default: Applies a standard style to the button.
. Cancel: Applies the Cancel style to the button, which means no change is made to the cancel operation.
. destructive: Apply a cautionary style to the button, prompting the user to do so may change or delete some data.
So you want to add a cautionary button to the modal alert, just add it. Destructive-style uialertaction properties:
| 123456789101112 |
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in println(action)}alertController.addAction(cancelAction)let destroyAction = UIAlertAction(title: "Destroy", style: .Destructive) { (action) in println(action)}alertController.addAction(destroyAction)self.presentViewController(alertController, animated: true) { // ...} |
Alert with a button greater than 2
When there are 1 or 2 operations, the buttons are arranged horizontally. More buttons will show up like action sheet:
| 12345678 |
let oneAction = UIAlertAction(title: "One", style: .Default) { (_) in }let twoAction = UIAlertAction(title: "Two", style: .Default) { (_) in}let threeAction = UIAlertAction(title: "Three", style: .Default) { (_) in}let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in}alertController.addAction(oneAction)alertController.addAction(twoAction)alertController.addAction(threeAction)alertController.addAction(cancelAction) |
Create a login form
IOS 5 added the Alertviewstyle attribute to Uialertview, exposing the original proprietary API to developers-like some system built-in apps that allow login and password boxes to be displayed in alert.
In IOS 8, Uialertcontroller added the Addtextfieldwithconfigurationhandler method:
| 123456789101112131415161718192021 |
let loginAction = UIAlertAction(title: "Login", style: .Default) { (_) in let loginTextField = alertController.textFields![0] as UITextField let passwordTextField = alertController.textFields![1] as UITextField login(loginTextField.text, passwordTextField.text)}loginAction.enabled = falselet forgotPasswordAction = UIAlertAction(title: "Forgot Password", style: .Destructive) { (_) in}let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in}alertController.addTextFieldWithConfigurationHandler { (textField) in textField.placeholder = "Login" NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object: textField, queue: NSOperationQueue.mainQueue()) { (notification) in loginAction.enabled = textField.text != "" }}alertController.addTextFieldWithConfigurationHandler { (textField) in textField.placeholder = "Password" textField.secureTextEntry = true}alertController.addAction(loginAction)alertController.addAction(forgotPasswordAction)alertController.addAction(cancelAction) |
Create a registration form
Uialertcontroller more thoughtful than before, it provides permission to display an unlimited number of input boxes, and each input box can be customized according to the requirements. This makes it possible to implement fully registered functions in only one modal alert:
| 123456789101112 |
alertController.addTextFieldWithConfigurationHandler { (textField) in textField.placeholder = "Email" textField.keyboardType = .EmailAddress}alertController.addTextFieldWithConfigurationHandler { (textField) in textField.placeholder = "Password" textField.secureTextEntry = true}alertController.addTextFieldWithConfigurationHandler { (textField) in textField.placeholder = "Password Confirmation" textField.secureTextEntry = true} |
Despite these, it must be explained that there is no need to overdo it. Not because you can do this, it means you should do it. Forget about it, provide a view controller to do the registration function, because you should have done so!
Attention
If you try to add an alert controller with a. The Actionsheet property's input box will throw an exception:
| 1 |
Terminating app due to uncaught exception NSInternalInconsistencyException, reason: ‘Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert‘ |
Similarly, add more than one to alert or action sheet. The Cancel button will throw an exception:
| 1 |
Terminating app due to uncaught exception NSInternalInconsistencyException, reason: ‘ |
Use of Uialertcontroller