IOS UIAlertController and uialertcontroller
In Xcode's iOS9.0 SDK, both UIAlertView and UIActionSheet are replaced by UIAlertController.
In iOS 9, UIAlertController is functionally the same as UIAlertView and UIActionSheet. UIAlertController replaces the functions and functions of the two products in a modular way. Whether the dialog box (alert) or the action sheet is used depends on how you set the preferred style when creating the controller.
1. Dialog Box
UIAlertController * alertController = [UIAlertController alert: @ "title" message: @ "this is the default style of UIAlertController" preferredStyle: alert]; [self presentViewController: alertController animated: YES completion: nil];
Compared with creating a UIAlertView, you do not need to specify a proxy or a button during initialization. However, pay special attention to the third parameter. Make sure that you select the dialog box style or the top menu style.
2. Add the action button to the Controller.
By creating an instance of UIAlertAction, you can add the action button to the Controller. UIAlertAction consists of the title string, style, and the code block that runs when the user selects the action. Through UIAlertActionStyle, you can select the following three action styles: regular (default), canceled (cancel), and warning (destruective ). To achieve the button effect we created when creating the UIAlertView, we only need to create these two Action Buttons and add them to the Controller.
UIAlertController * alertController = [UIAlertController alert: @ "title" message: @ "this is the default style of UIAlertController" preferredStyle: alert]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle: @ "cancel" style: incluhandler: nil]; UIAlertAction * okAction = [UIAlertAction actionWithTitle: @ "style" style: UIAlertActionStyleDefault handler: nil]; [alertController addAction: cancelAction]; [alertController addAction: okAction]; [self presentViewController: alertController animated: YES completion: nil];
3. "alert" Style
UIAlertController * alertController = [UIAlertController alert: @ "title" message: @ "this is the default style of UIAlertController" preferredStyle: alert]; UIAlertAction * resetAction = [UIAlertAction actionWithTitle: @ "reset" style: UIAlertActionStyleDestructive handler: nil]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle: @ "cancel" style: UIAlertActionStyleCancel handler: nil]; // The Order of adding and display is the same [alertController addAction: cancelAction]; [alertController addAction: resetAction]; [self presentViewController: alertController animated: YES completion: nil];
4. text dialog box
Set the input box and set the notification. If the length of the input character is less than 3, you cannot click "OK ".
It is worth noting that, unlike when using UIAlertView, you can use UIAlertViewDelegate to detect text box input. UIAlertController does not have a corresponding proxy and can only implement listening by itself.
UIAlertController * alertController = [UIAlertController login: @ "text dialog box" message: @ "logon and Password dialog box example" preferredStyle: Login]; [alertController addTextFieldWithConfigurationHandler: ^ (UITextField * textField) {[nsicationcenter center defaultCenter] addObserver: self selector: @ selector (alertTextFieldDidChange :) name: UITextFieldTextDidChangeNotification object: textField]; textField. placeholder = @ "login";}]; [alertController addTextFieldWithConfigurationHandler: ^ (UITextField * textField) {[[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (handler :) name: UITextFieldTextDidChangeNotification object: textField]; textField. placeholder = @ "password"; textField. secureTextEntry = YES;}]; UIAlertAction * okAction = [UIAlertAction actionWithTitle: @ "good" style: handle handler: ^ (UIAlertAction * action) {UITextField * login = alertController. textFields. firstObject; UITextField * password = alertController. textFields. lastObject; NSLog (@ "% @", login. text); NSLog (@ "% @", password. text) ;}]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle: @ "cancel" style: handle handler: nil]; [alertController addAction: cancelAction]; [alertController addAction: okAction]; [self presentViewController: alertController animated: YES completion: nil];
- (void)alertTextFieldDidChange:(NSNotification *)notification{ UIAlertController *alertController = (UIAlertController *)self.presentedViewController; if (alertController) { UITextField *login = alertController.textFields.firstObject; UIAlertAction *okAction = alertController.actions.lastObject; okAction.enabled = login.text.length > 2; }}
5,Pull up menu
UIAlertController * alertController = [UIAlertController resume: @ "Save or delete data" message: @ "data deletion will not be recoverable" preferredStyle: Restore]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle: @ "cancel" style: UIAlertActionStyleCancel handler: ^ (UIAlertAction * action) {NSLog (@ "click Cancel") ;}]; UIAlertAction * deleteAction = [UIAlertAction actionWithTitle: @ "delete" style: UIAlertActionStyleDestructive handler: ^ (UIAlertAction * action) {NSLog (@ "click to delete") ;}]; UIAlertAction * archiveAction = [UIAlertAction actionWithTitle: @ "save" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * action) {NSLog (@ "click to save") ;}]; [alertController addAction: cancelAction]; [alertController addAction: deleteAction]; [alertController addAction: archiveAction]; [self presentViewController: alertController animated: YES completion: nil];