Recently in the study of iOS development, learning Books "iOS7 pragramming Cookbook", the purpose of making notes for easy viewing. The form of a note is a small example of writing a complete example of a book.
Uialertviewclass Usage Scenarios
1, displays the information as a warning to the user.
2, let the user confirm some actions
3, let the user enter the user name and password
4, let the user enter some text, these text in the program is used
Example 1 Implementation simple display some warning message
Create a single View application simple project, project name Dimension Alterview, extended prefix CB
The code is as follows:
#import "CBViewController.h"@interfaceCbviewcontroller ()@end@implementationCbviewcontroller- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.Uialertview*alterview =[[Uialertview alloc] Initwithtitle:@"Learn some Mac/ios development"message:@"An example of a uialterview" Delegate: Self cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Determine", nil]; [Alterview show];}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Run results
Example 2 Let the user enter the account number and password, log output account number and password
To get the user clicking on the button is the following method that can be implemented in the code implementation uialertviewdelegate Agent
Called when a button is clicked. The view'll be automatically dismissed after this call returns
Used to click the button is called, return the index of the called button
-(void) Alertview: (uialertview *) Alertview clickedbuttonatindex: (nsinteger) buttonindex;
#import "CBViewController.h"@interfaceCbviewcontroller ()<UIAlertViewDelegate>//Add Agent@end@implementationCbviewcontroller- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.Uialertview*alterview =[[Uialertview alloc] Initwithtitle:@"Learn some Mac/ios development"message:@"Please enter your account number and password." Delegate: Self cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Determine", nil]; [Alterview Setalertviewstyle:uialertviewstyleloginandpasswordinput]; Uitextfield*user = [Alterview textfieldatindex:0 ] ; User.keyboardtype= Uikeyboardtypealphabet;//set the pop-up keyboard styleUitextfield*pass = [Alterview textfieldatindex:1] ; Pass.keyboardtype=Uikeyboardtypedefault; [Alterview show];}
////method of implementing proxy protocol ////Used to click the button is called, return the index of the called button- (void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) buttonindex{//get a realistic string of click buttons by returning a button indexNSString *buttonname =[Alertview Buttontitleatindex:buttonindex]; if([ButtonName isequaltostring:@"Determine"]) {Uitextfield*user = [Alertview textfieldatindex:0] ; Uitextfield*pass = [Alertview textfieldatindex:1] ; NSLog (@"determine the account =%@ password =%@", User.text, Pass.text); } Else if([ButtonName isequaltostring:@"Cancel"]) {NSLog (@"Cancel"); }}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Output results
2014-07-09 15:27:05.980 alertview[8406:60b] determine the account =goipc password =123456
2014-07-09 15:39:31.938 alertview[8429:60b] Cancel
Running the