IOS UIAlertView(警告框)方法总结

IOS中UIAlertView(警告框)经常使用方法总结

1、初始化方法

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;

这个方法经过设置一个标题,内容,代理和一些按钮的标题建立警告框,代码示例以下:atom

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"个人警告框" message:@"这是一个警告框" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil];
    [alert show];

效果以下:spa

注意:若是按钮数超过两个,将会建立成以下样子:.net

若是按钮数量超出屏幕显示范围,则会建立相似tableView的效果。3d

 

2、属性与方法解析

 

标题属性代理

@property(nonatomic,copy) NSString *title;code

内容属性索引

@property(nonatomic,copy) NSString *message;get

 

添加一个按钮,返回的是此按钮的索引值it

 

- (NSInteger)addButtonWithTitle:(NSString *)title;   table

返回根据按钮索引按钮标题 

- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

获取按钮数量

@property(nonatomic,readonly) NSInteger numberOfButtons;

设置将某一个按钮设置为取消按钮

@property(nonatomic) NSInteger cancelButtonIndex;

返回其余类型按钮第一个的索引值

@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;

警告框是否可见

@property(nonatomic,readonly,getter=isVisible) BOOL visible;

 

显现警告框

- (void)show;

代码模拟点击按钮消失触发方法

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

设置警告框风格

 

@property(nonatomic,assign) UIAlertViewStyle alertViewStyle;

风格的枚举以下

typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
    UIAlertViewStyleDefault = 0,//默认风格
    UIAlertViewStyleSecureTextInput,//密码输入框风格
    UIAlertViewStylePlainTextInput,//普通输入框风格
    UIAlertViewStyleLoginAndPasswordInput//帐号密码框风格
};

 

这个方法设置文本输入框的索引

- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex;

 

3、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;

专一技术,热爱生活,交流技术,也作朋友。

——珲少 QQ群:203317592

相关文章
相关标签/搜索