• 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:
  • 相关阅读:
    [RxJS] Combination operators: concat, startWith
    [RxJS] Filtering operators: skipWhile and skipUntil
    [RxJS] Filtering operators: takeUntil, takeWhile
    [RxJS] Filtering operators: takeLast, last
    强连通tarjan模版
    HDU 4576 Robot (概率DP)
    Inside GDALAllRegister之二: 自动加载驱动
    [置顶] Java中字符串为什么不以结尾
    rcp(插件开发) The 'Eclipse-LazyStart' header is deprecated, use 'Bundle-ActivationPolicy'
    如何在模板中引用参数类中的一个特定member
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4845152.html
Copyright © 2020-2023  润新知