1.UIAlertView
① simple agentless mode, the code is as follows:
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "hint" message:@ "This is a warning" delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", @ "button one", @ "button two", nil]; [Alertview show];//display is called without adding parent view [Alertview release];//memory Management
Here are several styles of several parameters
Uialertviewstyle This enumeration provides several styles Uialertviewstyledefault = 0,//default style uialertviewstylesecuretextinput,//ciphertext input Box Uialertviewstyleplaintextinput,//Clear text input box uialertviewstyleloginandpasswordinput//Login with input box, with clear text user name, and ciphertext password input two input box Uialertviewstylesecuretextinput and Uialertviewstyleplaintextinput can get the input box object by Textfieldindex. Uialertviewstyleloginandpasswordinput can get the user name input box object and the Password input box object by Textfieldindex for 0 and 1 respectively.
② has the proxy mode, the code is as follows:
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "hint" message:@ "This is a warning box" delegate:self cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", @ "button one", @ "button two", nil]; Set style style, with user name and password mode [Alertview setalertviewstyle:uialertviewstyleloginandpasswordinput]; Manually cancel the dialog box Operation [Alertview dismisswithclickedbuttonindex:0 animated:yes]; Get the corresponding input text box Uitextfield *text1 = [alert textfieldatindex:0]; Uitextfield *text2 = [alert textfieldatindex:1]; Sets the keyboard type of the text box Text1.keyboardtype = Uikeyboardtypenumberpad; Text2.keyboardtype = uikeyboardtypenumbersandpunctuation; [Alertview show]; [Alertview release];
Agent implementation (Uialertviewdelegate)
-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex { //Judging the user clicked the button, Depending on the index, you can add a response event at a different index here, nsstring *str = [[NSString alloc] initwithformat:@ "%ld buttons", (long) buttonindex]; NSLog (@ "%@", str);}
③uialertview All text is center-aligned by default. If you need to align text to the left or add other controls such as an input box, you should make the multiple delegate method, as follows
-(void) Willpresentalertview: (Uialertview *) Alertview { //before starting animation and presentation view for (UIView *view in Alertview.subviews) { if ([View Iskindofclass:[uilabel class]]) { UILabel *label = (UILabel *) view; Label.textalignment = Nstextalignmentcenter;}} }
Other proxy methods
-(void) Alertview: (Uialertview *) Alertview diddismisswithbuttonindex: (Nsinteger) Buttonindex { // This method is called after the end of the animation and the view is hidden ( void) Alertview: (Uialertview *) Alertview Willdismisswithbuttonindex: (Nsinteger) Buttonindex { //This method is called before the animation starts and the view is hidden
As follows:
2.UIActionSheet
Action Sheet: [The Action table (the important class that implements the warning box]] is like the OK-Cancel dialog box in Windows to force the user to make a selection. When a user is about to take a risk, the action sheet is often used to prompt the user for danger, so that the user has the opportunity to cancel the operation.
Alert is equivalent to the MessageBox in Windows and is similar to action sheet.
The difference is that alert can have only one selection, and action sheet has at least two options.
The code is as follows: The action method that is triggered when you click the button
-(void) buttonpressed: (ID) Sender { Uiactionsheet *actionsheet = [[Uiactionsheet alloc] initwithtitle:@ "is You sure? " Delegate:self cancelbuttontitle:@ "No way!" destructivebuttontitle:@ "Yes, I ' m sure!" Otherbuttontitles:nil]; Actionsheet.actionsheetstyle = uiactionsheetstyleblacktranslucent; [Actionsheet addbuttonwithtitle:@ "Item A"]; [Actionsheet addbuttonwithtitle:@ "Item B"]; [Actionsheet addbuttonwithtitle:@ "Item C"]; [Actionsheet ShowInView:self.view];}
Click Buttonm to trigger the effect as follows:
Note: If the last parameter is nil
[Actionsheet ShowInView:self.view]
This statement is used to show the action Sheet, which, to be exact, is to set the parent to the action Sheet, which must be a view and the view that is currently being displayed.
Note: did you see the red button on the other one? It is a so-called destruction button supported by Actionsheet, which acts as a warning to a certain action of a household.
For example, permanently delete a message or log. If you specify a Destroy button, he will highlight it in red.
Like the navigation bar, the action form also supports three flavors:
Uiactionsheetstyledefault//Default style: White text on gray background
Uiactionsheetstyleblacktranslucent//Transparent black background, white text
Uiactionsheetstyleblackopaque//Black background, white text
② Implementing proxy Methods (Uiactionsheetdelegate)
Handle the Click event according to the index of the click button, handle the corresponding action-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) buttonindex{ NSLog (@ "%d", buttonindex); if (Buttonindex = = Actionsheet.cancelbuttonindex) { return; } Switch (buttonindex) {case 0:{ NSLog (@ "Item aselected"); break; } Case 1:{ NSLog (@ "Item B Selected"); break; } Case 2:{ NSLog (@ "Item C Selected"); Break;}}}
Trigger when Uiactionsheet has disappeared
Actionsheet has disappeared-(void) Actionsheet: (Uiactionsheet *) Actionsheetdiddismisswithbuttonindex: (Nsinteger) Buttonindex { //when clicking the Cancel button executes if (buttonindex! = [Actionsheet Cancelbuttonindex]) { Uialertview *alert = [[UIA] Lertview alloc] initwithtitle:@ "Something was did" message:@ "You can breathe easy, everything went ok." Delegate:self cancelbuttontitle:@ "prew!" Otherbuttontitles:nil]; [Alert show]; } }
After clicking Cancel.
The above method is the code we will execute after we touch the action sheet. Since we previously set the action sheet delegate to self, this method will be called, and the parameter of this method buttonindex the number of the button that the user has touched, the button number is from top to bottom, starting from 0, for example, "Yes, I m sure!" This button is numbered 0 because it is the first OK button and the Cancel button is displayed at the bottom. The number of the Cancel button can be obtained directly from [Actionsheet Cancelbuttonindex].
③uialertcontroller (iOS8 new features)
IOS8, Uiacticonsheet has been discarded, and uiactionsheet-based customizations will be ineffective.
Apple integrates Uiactionsheet and Uialertview into an interface uialertcontroller.
There are two ways of creating an object
1.UIAlertControllerStyleAlert type
2.UIAlertControllerStyleActionSheet type
Comparison:
1. The former frame form, the latter drop-down box form
2. The former can add more than one TextField input box, the latter can not
3. The former input box has all the properties of the TextField, can be set according to different needs of the frame and keyboard format
The first way code is as follows:
Uialertcontroller *alertcontroller = [uialertcontroller alertcontrollerwithtitle:@ "title" message:@ "General style" preferredstyle:uialertcontrollerstylealert]; [Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) { Textfield.placeholder = @ "Password"; textfield.securetextentry = yes; }]; Add input Box [alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) { Textfield.placeholder = @ "Login"; }];//can do the TextField in the block [ Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) { Textfield.placeholder = @ "Please enter your password"; textfield.securetextentry = yes;//input Box ciphertext display format }]; Add other buttons uialertaction *actiona = [uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel handler:^ (uialertaction *action) {//corresponding to each button handling event action & nbsp; NSLog (@ "Click to cancel"); }];//can handle the corresponding event in the block of the corresponding action, without using the Proxy method Uialertaction * ACTIONB = [uialertaction actionwithtitle:@ "OK" style:uialertactionstyledestructive handler:^ (UIAlertAction *a ction) { NSLog (@ "clicked OK"); }]; uialertaction *actionC = [ Uialertaction actionwithtitle:@ "button One" style:uialertactionstyledestructive handler:^ (uialertaction *action) {&N bsp; NSLog (@ "click button One"); }]; three styles of style for/** * uialertaction objects: //default format 1.uialertactionstyledefault //Cancel operation. This format can only be used by one Uialertaction object, not more than two uialertactionstylecancel //Warning style, Button color red, remind user to do this may change or delete some data uialertactionstyledestructive *//Add action [ Alertcontroller addaction:actiona];//Add action to Alertcontroller [AlertController addAction: actionb]; [Alertcontroller Addaction:actionc]; Method [self presentviewcontroller:alertcontroller animated:yes completion:nil];As follows:
Note: 1. Although Uialertcontroller is an upgraded version of Uialertview and Uiactionsheet, it is more flexible and easier to operate, but the original Uialertview and Uiactionsheet classes can still be used in iOS8 Well done, and can still be used at the moment.
2. Although Uialertcontroller can flexibly add the number of TextField and according to the requirements to set its properties, but can not be considered to be able to use Uialertcontroller to complete the registration of the user name, and other operations, User name registration and login and other operations to use Viewcontroller to complete, after all, Uialertcontroller is not used for this kind of "expert"
"Learning the path to iOS: UI series" Uialertview. Uiactionsheet and Uialertcontroller