A brief introduction to Uialertview
If you need to pop up a message for the user to confirm, or require the user to enter the account password, other this article, you can use Uialertview.
Two Uialertview creation
/** 1. Create UIAlertView title Tip View title, such as alarms, hints, exceptions message The actual information the user sees delegate An optional parameter that passes the delegate object to the prompt view, and the delegate object is notified when the view state changes. The passed parameter object must implement the UIAlertViewDelegate contract cancelButtonTitle Optional parameters, which are displayed on the Cancel button of the hint view. otherButtonTitles Optional parameters, if you want the hint view to appear other buttons, as long as the title parameter is passed, this parameter needs to be separated by commas, with the end of nil . */ uialertview *alertview = [[uialertview alloc] initwithtitle:@ "Title" message:@ " Message " delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil];
Three set style
/** 2. Set style Uialertviewstyledefault = 0, default, no input box uialertviewstylesecuretextinput, hint view add password box Uiale Rtviewstyleplaintextinput, add input box to the prompt view Uialertviewstyleloginandpasswordinput login and Password box */[Alertview Setalertviewstyle:uialertviewstyleloginandpasswordinput];
Four Show
[Alertview show];
Five taps and gets the user's input
If you want to listen to the user's clicks and get user input, you need to implement the Uialertviewdelegate protocol, the Alertview:clickedbuttonatindex method in the protocol can get the button that the user presses on the prompt view, The index value of the button is stored in the variable Clickedatindex
/** * Monitor Click * * @param alertView < #alertView description#> * @param buttonIndex < #buttonIndex description#> */-(void) Alertview: (uialertview *) Alertview clickedbuttonatindex: (Nsinteger) buttonIndex{ NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex]; // Judgment Click if ([Buttontitle isequaltostring:@ "Cancel"]) { nslog (@ "User pressed the cancel button. "); } else if ([buttontitle isequaltostring:@ "OK"]) { nslog (@ "User pressed the ok button."); } //accepts input class capacity &Nbsp; //textfieldatindex get the uitextfield uitextfield * of the corresponding position Textfield = [alertview textfieldatindex:0]; nslog (@ "%@", TextField.text); UITextField *textField2 = [alertView Textfieldatindex:1]; nslog (@ "%@", Textfield2.text);}
Six complete code
#import "ViewController.h" @interface ViewController () <uialertviewdelegate>@ end@implementation viewcontroller- (void) viewdidload { [super viewdidload]; //uialertview effect //If you need to pop up a message to let the user confirm, or require the user to enter the account password, other this article, you can use uialertview /** 1. Create UIAlertView title Tip View title, such as alarms, hints, exceptions message The actual information the user sees delegate Optional Parameters, The delegate object is passed to the prompt view, and the delegate object is notified when the view state changes. The passed parameter object must be implemented uialertviewdelegate Agreement cancelButtonTitle Optional parameters, which are displayed on the Cancel button in the prompt view. otherButtonTitles Optional parameters, if you want the hint view to appear other buttons, as long as the title parameter is passed, this parameter needs to be separated by commas, with the end of nil . */ uialertview * alertview = [[uialertview alloc] initwithtitle:@ "Title" message:@ "Message" delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil]; /** 2. Set Style UIAlertViewStyleDefault = 0, default, no input box Add a password box to the uialertviewstylesecuretextinput, hint view uialertviewstyleplaintextinput, Add input box to the tips view uialertviewstyleloginandpasswordinput Login and Password box */ [alertview setalertviewstyle:uialertviewstyleloginandpasswordinput] ; /** 3. Monitoring Click if you want to monitor the user's clicks to implement the uialertviewdelegate protocol, the Alertview in the protocol: The clickedbuttonatindex method can get the button that the user presses on the prompt view, the index value of the button is stored in the variable clickedAtIndex */ [alertview setdelegate:self];// uialertview * alertview = [[uialertview alloc] initwithtitle:@ "Title"// message:@ "Message"// delegate:self// cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil]; /** 4. accepts alertview input class capacity */ //Show [alertView show]; }/** * Monitor Click * * @param alertView < #alertView description#> * @ param buttonindex < #buttonIndex description#> */-(void) Alertview: (uialertview * ) Alertview clickedbuttonatindex: (Nsinteger) Buttonindex{ nsstring *buttontitle = [alertview buttontitleatindex:buttonindex]; / / Judgment Click if ([buttontitle isequaltostring:@ "Cancel"]) { nslog (@ "User pressed the cancel button."); } else if ([buttontitle isequaltostring:@ "OK"]) { nslog (@ "User pressed the ok button."); } //accept Input class capacity UITextField *textField = [alertView textFieldAtIndex:0]; nslog (@ "%@", textfield.text); uitextfield *textfield2 = [alertview textfieldatindex:1]; nslog (@ "%@", Textfield2.text);} @end
IOS Uialertview Tips View