天天看点

iOS:提示框(警告框)控件UIAlertView的详解

提示框(警告框)控件:UIAlertView

功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能。

类型:typedef NS_ENUM(NSInteger, UIAlertViewStyle) {

    UIAlertViewStyleDefault = 0,                 //默认类型     UIAlertViewStyleSecureTextInput,          //安全密码的文本框输入类型     UIAlertViewStylePlainTextInput,            //普通文本框的文本框输入类型     UIAlertViewStyleLoginAndPasswordInput //登陆账号和密码输入类型    };

属性:

@property(nonatomic,assign) id <UIAlertViewDelegate> delegate;  //提示框代理

@property(nonatomic,copy) NSString *title;                              //提示框标题

@property(nonatomic,copy) NSString *message;                        // 提示信息

@property(nonatomic,readonly) NSInteger numberOfButtons;       // 提示框的按钮数量

@property(nonatomic) NSInteger cancelButtonIndex;                  // 提示框上被点击的按钮的索引

@property(nonatomic,readonly) NSInteger firstOtherButtonIndex; // 第一个其他按钮的索引 

@property(nonatomic,readonly,getter=isVisible) BOOL visible;      // 提示框是否可见

@property(nonatomic,assign) UIAlertViewStyle alertViewStyle ;    // 提示框类型

注意:如果按钮数量超出屏幕显示范围,则会创建类似tableView的效果。

对象方法:

 ※初始化提示框

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

※添加一个按钮,返回该按钮的索引值

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

※返回指定索引值的提示框标题

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

※显示提示框

-(void)show;

※点击指定的按钮时提示框消失

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

※设置输入文本框的索引,返回文本框

- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex

协议:代理方法

protocol UIAlertViewDelegate <NSObject>

@optional

※点击提示框上的按钮时触发的方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

※提示框将要消失时触发的方法

- (void)alertViewCancel:(UIAlertView *)alertView;

※提示框将要显示时触发的方法

- (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;

@end

具体的举例如下:

首先在视图控制器视图中创建按钮并添加事件,便于完成提示框的创建:

iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解

1、第一种形式:提示框上添加一两个按钮的默认类型

iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解

演示结果:

iOS:提示框(警告框)控件UIAlertView的详解

2、第二种形式:提示框上添加多个按钮的默认类型

iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解

3、第三种形式:提示框的类型为普通文本框输入类型

iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解

4、第四种形式:提示框的类型为安全文本框输入类型

iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解

5、第五种形式:提示框的类型为登陆账号和密码文本框输入类型

iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解

协议代理的方法的使用:

1、首先实现协议:@interface ViewController ()<UIAlertViewDelegate>

2、其次设置代理,以上面的第一种形式提示框举例:

iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解

3、实现协议的方法:

iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解

刚开始时View中的按钮截图:               

iOS:提示框(警告框)控件UIAlertView的详解

点击"点击"按钮时,截图中其他第一个按钮被设置为不是取消按钮(按钮无效)和代理方法执行顺序结果:

iOS:提示框(警告框)控件UIAlertView的详解

点击"确认"时,剩下的代理方法执行顺序的结果:

总的执行结果为:

iOS:提示框(警告框)控件UIAlertView的详解
iOS:提示框(警告框)控件UIAlertView的详解

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!

本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4845152.html,如需转载请自行联系原作者

继续阅读