Summary: After iOS8, the Uialertview and Uiactionsheet controls are replaced by Uialertcontroller, but the two controls are still available. This blog mainly summarizes the common methods of Uialertview. IOS9 Uialertview can't be used.
first, the method of initialization-(Instancetype) Initwithtitle: (NSString *) title message: (nsstring*) message delegate: (ID/*< uialertviewdelegate>*/) Delegate Cancelbuttontitle: (NSString *) cancelbuttontitle otherbuttontitles: (NSString *) Otherbuttontitles, ...;
This method creates a warning box by setting a title, content, proxy, and some button titles, and the code example is as follows:
?
| 1 2 |
uialertview * alert = [[uialertview alloc]initwithtitle:@ "my warning box" message:@ "This is a warning box" delegate:self cancelbuttontitle:@ otherbuttontitles: @ , nil]; &NBSP;&NBSP;&NBSP;&NBSP; [alert show]; |
The effect is as follows:
Note: If the number of buttons is more than two, it will be created like this:
If the number of buttons exceeds the screen display range, an effect similar to TableView will be created.
Second, attribute and method analysis
Title Property
@property (nonatomic,copy) NSString *title;
Content Properties
@property (nonatomic,copy) NSString *message;
Add a button that returns the index value of this button
-(Nsinteger) Addbuttonwithtitle: (NSString *) title;
Returns the button title according to the button index
-(NSString *) Buttontitleatindex: (Nsinteger) Buttonindex;
Get the number of buttons
@property (nonatomic,readonly) Nsinteger numberofbuttons;
Set set a button to cancel button
@property (nonatomic) Nsinteger Cancelbuttonindex;
Returns the index value of the first of the other type buttons
@property (nonatomic,readonly) Nsinteger Firstotherbuttonindex;
Whether the warning box is visible
@property (nonatomic,readonly,getter=isvisible) BOOL visible;
Show Warning Box
-(void) show;
Code simulation Click Button Disappears Trigger method
-(void) Dismisswithclickedbuttonindex: (Nsinteger) Buttonindex animated: (BOOL) animated;
Set Alert box style
@property (nonatomic,assign) Uialertviewstyle Alertviewstyle;
The enumeration of styles is as follows
| 1 2 3 4 5 6 |
typedef NS_ENUM(NSInteger, UIAlertViewStyle) { UIAlertViewStyleDefault = 0,//默认风格 UIAlertViewStyleSecureTextInput,//密码输入框风格 UIAlertViewStylePlainTextInput,//普通输入框风格 UIAlertViewStyleLoginAndPasswordInput//账号密码框风格}; |
This method sets the index of the text input box
-(Uitextfield *) Textfieldatindex: (Nsinteger) Textfieldindex;
three, the method in Uialertviewdelegate
How to trigger when a button is clicked
-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex;
The method to trigger when the warning box will be displayed
-(void) Willpresentalertview: (Uialertview *) Alertview;
Method triggered when a warning box has been displayed
-(void) Didpresentalertview: (Uialertview *) Alertview;
The method that is triggered when the warning box is going to disappear
-(void) Alertview: (Uialertview *) Alertview Willdismisswithbuttonindex: (Nsinteger) Buttonindex;
Method that is triggered when the warning box has disappeared
-(void) Alertview: (Uialertview *) Alertview Diddismisswithbuttonindex: (Nsinteger) Buttonindex;
Sets whether the first button is allowed instead of the Cancel button
-(BOOL) Alertviewshouldenablefirstotherbutton: (Uialertview *) Alertview;
Summary of common methods for Uialertview (warning box) in iOS