Summary of common methods of UIAlertView (alert box) in IOS
One, initialization method
- (instancetype)initWithTitle:(NSString *)title message:(NSString*)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;
This method creates an alert box by setting a title, content, proxy, and some button titles. The code example is as follows:
1
2
|
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@
"我的警告框"
message:@
"这是一个警告框"
delegate:self cancelButtonTitle:@
"取消"
otherButtonTitles:@
"确定"
, nil];
[alert show];
|
The effect is as follows:
Note: If there are more than two buttons, they will be created as follows:
If the number of buttons exceeds the screen display range, an effect similar to tableView will be created.
2. Attribute and method analysis
Title attribute
@property(nonatomic,copy) NSString *title;
Content attribute
@property(nonatomic,copy) NSString *message;
Add a button and return 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 a button as a cancel button
@property(nonatomic) NSInteger cancelButtonIndex;
Returns the index value of the first button of other types
@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;
Whether the warning box is visible
@property(nonatomic,readonly,getter=isVisible) BOOL visible;
显现警告框
- (void)show;
代码模拟点击按钮消失触发方法
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
设置警告框风格
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle;
风格的枚举如下
1
2
3
4
5
6
|
typedef
NS_ENUM(NSInteger, UIAlertViewStyle) {
UIAlertViewStyleDefault = 0,
//默认风格
UIAlertViewStyleSecureTextInput,
//密码输入框风格
UIAlertViewStylePlainTextInput,
//普通输入框风格
UIAlertViewStyleLoginAndPasswordInput
//账号密码框风格
};
|
这个方法设置文本输入框的索引
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex;
三、UIAlertViewDelegate中的方法
点击按钮时触发的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
将要展现警告框时触发的方法
- (void)willPresentAlertView:(UIAlertView *)alertView;
已经展现警告框时触发的方法
- (void)didPresentAlertView:(UIAlertView *)alertView;
警告框将要消失时触发的方法
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
警告框已经消失时触发的方法
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
设置是否允许第一个按钮不是取消按钮
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;