IOS UIAlertView 提示视图

一 UIAlertView 简介    

如果需要弹出讯息让用户确认,或者要求用户输入帐户密码,其他本文,则可用用UIAlertView。


二 UIAlertView 创建

    /**
     1.创建 UIAlertView
     
     title                  提示视图标题,比如 告警、提示、异常
     message                用户看的实际讯息
     delegate               可选参数,传递委托对象给提示视图,当视图状态变更时,委托对象会被通知。传递的参数对象必须实现 UIAlertViewDelegate 协定
     cancelButtonTitle      可选参数,这个字符串符会显示在提示示视图的取消按钮上。     
     otherButtonTitles      可选参数,若你希望提示示视图出现其他按钮,只要传递标题参数,此参数需用逗号分隔,用 nil 做结尾。
     
     */
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                                        message:@"Message"
                                                       delegate:nil
                                               cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

  

    

三 设置样式

  /**
     2. 设置样式
     UIAlertViewStyleDefault = 0,            默认,没有输入框
     UIAlertViewStyleSecureTextInput,        提示视图中添加密码框
     UIAlertViewStylePlainTextInput,         提示视图中添加输入框
     UIAlertViewStyleLoginAndPasswordInput   登录和密码框
     
     */
    [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

    

    

四 展示 

    [alertView show];

    



五 监听点击,并获取用户的输入

如果要监听用户的点击和获取用户输入,需要实现UIAlertViewDelegate 协议,协议中的alertView:clickedButtonAtIndex 方法可以得到用户在提示视图上所按的按钮,按钮的索引值会被储存在变量 clickedAtIndex 中

/**
 *  监听点击
 *
 *  @param alertView   <#alertView description#>
 *  @param buttonIndex <#buttonIndex description#>
 */
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    
    // 判断点击
    if ([buttonTitle isEqualToString:@"Cancel"]){
        NSLog(@"User pressed the Cancel button.");
    }
    else if ([buttonTitle isEqualToString:@"Ok"]){
        NSLog(@"User pressed the Ok button.");
    }
    
    
    //接受输入类容
    //textFieldAtIndex 获取对应位置的UITextField
    UITextField *textField = [alertView textFieldAtIndex:0];
    NSLog(@"%@",textField.text);
    
    UITextField *textField2 = [alertView textFieldAtIndex:1];
    NSLog(@"%@",textField2.text);
}




六 完整代码

#import "ViewController.h"

@interface ViewController ()<UIAlertViewDelegate>

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //UIAlertView 作用
    //如果需要弹出讯息让用户确认,或者要求用户输入帐户密码,其他本文,则可用用UIAlertView
    
    /**
     1.创建 UIAlertView
     
     title                  提示视图标题,比如 告警、提示、异常
     message                用户看的实际讯息
     delegate               可选参数,传递委托对象给提示视图,当视图状态变更时,委托对象会被通知。传递的参数对象必须实现 UIAlertViewDelegate 协定
     cancelButtonTitle      可选参数,这个字符串符会显示在提示示视图的取消按钮上。     
     otherButtonTitles      可选参数,若你希望提示示视图出现其他按钮,只要传递标题参数,此参数需用逗号分隔,用 nil 做结尾。
     
     */
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                                        message:@"Message"
                                                       delegate:nil
                                               cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    
    
    
    /**
     2. 设置样式
     UIAlertViewStyleDefault = 0,            默认,没有输入框
     UIAlertViewStyleSecureTextInput,        提示视图中添加密码框
     UIAlertViewStylePlainTextInput,         提示视图中添加输入框
     UIAlertViewStyleLoginAndPasswordInput   登录和密码框
     
     */
    [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    
    
    
    
    
    /**
     3. 监听点击
     
     如果要监听用户的点击需要实现UIAlertViewDelegate 协议,协议中的alertView:clickedButtonAtIndex 方法可以得到用户在提示视图上所按的按钮,按钮的索引值会被储存在变量 clickedAtIndex 中
     */
    [alertView setDelegate:self];
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
//                                                        message:@"Message"
//                                                       delegate:self
//                                              cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

    
    /**
     4. 接受AlertView 输入类容
     */
    
    
    
    //展示
    [alertView show];
    

}



/**
 *  监听点击
 *
 *  @param alertView   <#alertView description#>
 *  @param buttonIndex <#buttonIndex description#>
 */
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    
    // 判断点击
    if ([buttonTitle isEqualToString:@"Cancel"]){
        NSLog(@"User pressed the Cancel button.");
    }
    else if ([buttonTitle isEqualToString:@"Ok"]){
        NSLog(@"User pressed the Ok button.");
    }
    
    
    //接受输入类容
    UITextField *textField = [alertView textFieldAtIndex:0];
    NSLog(@"%@",textField.text);
    
    UITextField *textField2 = [alertView textFieldAtIndex:1];
    NSLog(@"%@",textField2.text);
}


@end


转载于:https://my.oschina.net/wolx/blog/393833

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值