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

 

具体的举例如下:

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

复制代码
 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     
 4     
 5     //在视图中添加按钮事件
 6     UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
 7     
 8     [button setTitle:@"点击" forState:UIControlStateNormal];
 9     
10     [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
11     
12     button.center = self.view.center;
13     
14     //第一种形式:提示框上添加一两个按钮的默认类型
15     //[button addTarget:self action:@selector(Clicked_1:) forControlEvents:UIControlEventTouchUpInside];
16     
17     //第二种形式:提示框上添加多个按钮的默认类型
18     //[button addTarget:self action:@selector(Clicked_2:) forControlEvents:UIControlEventTouchUpInside];
19     
20     //第三种形式:提示框的类型为普通文本框输入类型
21     //[button addTarget:self action:@selector(Clicked_3:) forControlEvents:UIControlEventTouchUpInside];
22     
23     //第四种形式:提示框的类型为安全文本框输入类型
24     //[button addTarget:self action:@selector(Clicked_4:) forControlEvents:UIControlEventTouchUpInside];
25     
26     //第五种形式:提示框的类型为登陆账号和密码文本框输入类型
27     [button addTarget:self action:@selector(Clicked_5:) forControlEvents:UIControlEventTouchUpInside];
28     
29     [self.view addSubview:button];
30 }
复制代码

 

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

复制代码
1 #pragma mark -Clicked1  提示框上添加一两个按钮的默认类型
2 -(void)Clicked_1:(UIButton*)sender
3 {
4     //创建提示框对象
5     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"信息输入有误" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
6     
7     //显示提示框
8     [alertView show];
9 }
复制代码

演示结果:

 

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

复制代码
 1 #pragma mark -Clicked2  提示框上添加多个按钮的默认类型
 2 -(void)Clicked_2:(UIButton*)sender
 3 {
 4     //创建提示框对象
 5     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"多个按钮" delegate:self cancelButtonTitle:@"button1" otherButtonTitles:@"button2", nil];
 6     
 7     //继续添加按钮
 8     [alertView addButtonWithTitle:@"button3"];
 9     [alertView addButtonWithTitle:@"button4"];
10     [alertView addButtonWithTitle:@"button5"];
11     [alertView addButtonWithTitle:@"button6"];
12     [alertView addButtonWithTitle:@"button7"];
13     
14     //显示提示框
15     [alertView show];
16 }
复制代码

演示结果:

 

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

复制代码
 1 #pragma mark -Clicked_3  提示框的类型为普通文本框输入类型
 2 -(void)Clicked_3:(UIButton*)sender
 3 {
 4     //创建提示框对象
 5     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"请输入信息" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
 6     
 7     alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
 8     
 9     //显示提示框
10     [alertView show];
11 }
复制代码

演示结果:

 

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

复制代码
 1 #pragma mark -Clicked_4  提示框的类型为安全文本框输入类型
 2 -(void)Clicked_4:(UIButton*)sender
 3 {
 4     //创建提示框对象
 5     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"请输入信息" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
 6     
 7     alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
 8     
 9     //显示提示框
10     [alertView show];
11 }
复制代码

演示结果:

 

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

复制代码
 1 #pragma mark -Clicked_5  提示框的类型为登陆账号和密码文本框输入类型
 2 -(void)Clicked_5:(UIButton*)sender
 3 {
 4     //创建提示框对象
 5     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"请输入账号和密码" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
 6     
 7     alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
 8     
 9     //显示提示框
10     [alertView show];
11 }
复制代码

演示结果:

 

协议代理的方法的使用:

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

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

复制代码
 1 #pragma mark -Clicked1  提示框上添加一两个按钮的默认类型
 2 -(void)Clicked_1:(UIButton*)sender
 3 {
 4     //创建提示框对象
 5     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"信息输入有误" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
 6     
 7     alertView.delegate = self; //设置代理
 8     
 9     //显示提示框
10     [alertView show];
11 }
复制代码

3、实现协议的方法:

复制代码
 1 #pragma mark -<UIAlertViewDelegate>
 2 //※点击提示框上的按钮时触发的方法
 3 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 4 {
 5     NSLog(@"alertView:clickedButtonAtIndex:");
 6 }
 7 
 8 //※提示框将要消失时触发的方法
 9 - (void)alertViewCancel:(UIAlertView *)alertView
10 {
11     NSLog(@"alertViewCancel:");
12 }
13 
14 //※提示框将要显示时触发的方法
15 - (void)willPresentAlertView:(UIAlertView *)alertView
16 {
17     NSLog(@"willPresentAlertView:");
18 }
19 
20 //※已经显示提示框时触发的方法
21 - (void)didPresentAlertView:(UIAlertView *)alertView
22 {
23      NSLog(@"didPresentAlertView:");
24 }
25 
26 //※提示框将要消失是触发的方法
27 - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
28 {
29     NSLog(@"alertView: willDismissWithButtonIndex:");
30 }
31 
32 //※提示框已经消失时触发的方法
33 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
34 {
35     NSLog(@"alertView: didDismissWithButtonIndex:");
36 }
37 
38 //※设置提示框的第一个按钮是否不是取消按钮
39 - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
40 {
41     NSLog(@"NO");
42     return NO;
43 }
复制代码

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

         

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

     

2015-09-28 23:41:30.239 提示框(警告框)UIAlertView[4383:427330] willPresentAlertView:
2015-09-28 23:41:30.240 提示框(警告框)UIAlertView[4383:427330] NO
2015-09-28 23:41:30.763 提示框(警告框)UIAlertView[4383:427330] didPresentAlertView:

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

复制代码
2015-09-28 23:48:27.366 提示框(警告框)UIAlertView[4383:427330] alertView:clickedButtonAtIndex:
2015-09-28 23:48:27.367 提示框(警告框)UIAlertView[4383:427330] alertView: willDismissWithButtonIndex:
2015-09-28 23:48:27.777 提示框(警告框)UIAlertView[4383:427330] alertView: didDismissWithButtonIndex:
复制代码

 

总的执行结果为:

复制代码
2015-09-28 23:41:30.239 提示框(警告框)UIAlertView[4383:427330] willPresentAlertView:
2015-09-28 23:41:30.240 提示框(警告框)UIAlertView[4383:427330] NO
2015-09-28 23:41:30.763 提示框(警告框)UIAlertView[4383:427330] didPresentAlertView:
2015-09-28 23:48:27.366 提示框(警告框)UIAlertView[4383:427330] alertView:clickedButtonAtIndex:
2015-09-28 23:48:27.367 提示框(警告框)UIAlertView[4383:427330] alertView: willDismissWithButtonIndex:
2015-09-28 23:48:27.777 提示框(警告框)UIAlertView[4383:427330] alertView: didDismissWithButtonIndex:
复制代码

 

 

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

本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4845152.html,如需转载请自行联系原作者
相关文章
|
10月前
|
iOS开发
iOS 多个滚动控件嵌套Demo
iOS 多个滚动控件嵌套Demo
53 0
|
10月前
|
iOS开发
iOS 常用的 上下左右 拉刷新控件
iOS 常用的 上下左右 拉刷新控件
68 0
|
iOS开发
iOS短信验证码控件,自动输入回调两次解决办法
iOS短信验证码控件,自动输入回调两次解决办法
382 0
|
iOS开发
IOS的UITableView控件简单使用
IOS的UITableView控件简单使用
142 0
|
iOS开发
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
138 0
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
|
安全 iOS开发
iOS小技能:下拉刷新控件的适配
1. 下拉顶部背景色设置: 往tableView的父控件添加拉伸背景视图 2. present 半屏适配 iOS13 modalPresentationStyle属性默认不是全屏样式`UIModalPresentationFullScreen`,而是半屏样式,需要根据需求手动设置。 present 半屏,会导致列表下拉刷新失效。
179 0
iOS小技能:下拉刷新控件的适配
|
iOS开发
iOS小技能:自动布局实现兄弟控件N等分且宽高比例是1:N(xib 上实现)
本文为 iOS视图约束专题的第三篇:xib上使用自动布局教程
157 0
|
iOS开发
iOS开发-加在透明视图上的控件会透明
iOS开发-加在透明视图上的控件会透明
126 0
|
存储 前端开发 程序员
iOS开发:实现点击常用控件弹出地区选择框(万能方法)
在iOS开发中会遇到一些选择选项的需求,而且点击一个控件弹出一个选择框,选择之后展示到前端,然后再把选择的内容传给后台或者做本地存储。这个需求对于大多数开发者来说可以为小儿科,但是作为一个爱记录的程序猿来说相当可贵,所以还是那句话,只分享给有缘人,大牛可以飘过,不喜勿喷请走开。
420 0
iOS开发:实现点击常用控件弹出地区选择框(万能方法)
|
数据可视化 程序员 iOS开发
iOS开发:用XIB拖控件关联时报错:“Could not insert new outlet connection…”解决方法
在iOS开发过程中,尤其是iOS开发初期,会遇到各种各样的错误,有些错误是开发者的不熟悉或者疏忽大意造成的,还有些是无厘头的错误,可以通过重启Xcode或者重启电脑就可解决。
232 0
iOS开发:用XIB拖控件关联时报错:“Could not insert new outlet connection…”解决方法