IOS development and ios development tutorial
Traditional alertView
-(Void) alertView {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "warning" message: @ "Your operation is invalid. Do you want to continue?" delegate: nil cancelButtonTitle: @ "cancel" otherButtonTitles: @ "OK", nil]; alert. alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; [alert show];}
Traditional actionSheet
-(Void) actionSheet {UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle: @ "Warning: Your operation is invalid. Do you want to continue?" delegate: nil cancelButtonTitle: @ "cancel" destructiveButtonTitle: @ "OK" otherButtonTitles: @ "close", nil]; [sheet showInView: self. view];}
UIAlertController = UIAlertView + UIActionSheet
-(Void) alertController {// dangerous operation: pop-up reminder // 1. UIAlertView // 2. UIActionSheet // start with iOS8: UIAlertController = UIAlertView + UIActionSheet UIAlertController * alert = [UIAlertController alertControllerWithTitle: @ "warning" message: @ "Your operation is invalid, do you want to continue? "preferredStyle: UIAlertControllerStyleAlert]; // Add button _ weak typeof (alert) weakAlert = alert; [alert addAction: [UIAlertAction actionWithTitle: @" OK "style: UIAlertActionStyleDestructive handler: ^ (UIAlertAction * action) {NSLog (@ "click the OK button -- % @-% @", [weakAlert. textFields. firstObject text], [weakAlert. textFields. lastObject text]);}]; [alert addAction: [UIAlertAction actionWithTitle: @ "cancel" style: UIAlertActionStyleCancel handler: ^ (UIAlertAction * action) {NSLog (@ "click the cancel button") ;}]]; [alert addAction: [UIAlertAction actionWithTitle: @ "other" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * action) {NSLog (@ "Click other buttons") ;}]]; // Add the text box [alert addTextFieldWithConfigurationHandler: ^ (UITextField * textField) {textField. textColor = [UIColor redColor]; textField. text = @ "123"; [textField addTarget: self action: @ selector (usernameDidChange :) forControlEvents: UIControlEventEditingChanged]; // [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (usernameDidChange :) name: UITextFieldTextDidChangeNotification object: textField] ;}]; [alert addTextFieldWithConfigurationHandler: ^ (UITextField * textField) {textField. secureTextEntry = YES; textField. text = @ "123";}]; [self presentViewController: alert animated: YES completion: nil];}-(void) usernameDidChange :( UITextField *) username {NSLog (@ "% @", username. text );}
Other operations
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {UIAlertController * alert = [UIAlertController alertControllerWithTitle: @ "warning" message: @ "Your operation is invalid, do you want to continue? "preferredStyle: UIAlertControllerStyleActionSheet]; // set the item alert to which popover points. popoverPresentationController. barButtonItem = self. navigationItem. leftBarButtonItem; // Add button [alert addAction: [UIAlertAction actionWithTitle: @ "OK" style: UIAlertActionStyleDestructive handler: ^ (UIAlertAction * action) {NSLog (@ "Click OK") ;}]]; [alert addAction: [UIAlertAction actionWithTitle: @ "cancel" style: UIAlertActionStyleCancel handler: ^ (UIAlertAction * action) {NSLog (@ "click the cancel button") ;}]]; [self presentViewController: alert animated: YES completion: nil] ;}// UIAlertControllerStyleActionSheet Usage Note // 1. no text box // 2. in iPad, you must use popover to display // Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert // you can only add Text boxes to views in the UIAlertControllerStyleAlert style.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.