UIAlertView的使用

// 方式1 无代理,只有一个确定按钮(左右排列,取消按钮在左,确定按钮在右)
UIAlertView *alertview001 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"知道了", nil];
[alertview001 show];


// 方式2 无代理,有多个确定按钮(多个确定按列表排列,且取消按钮在最下面)
UIAlertView *alertview002 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"知道0", @"知道1", @"知道2", @"知道3", @"知道4", nil];
[alertview002 show];


// 方法3 有代理,只有一个确定按钮
/*
1 设置代理为 self
2 设置代理协议
3 实现代理方法
*/
UIAlertView *alertview003 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"知道0", nil];
[alertview003 show];

// 设置协议
@interface ViewController () <UIAlertViewDelegate>

@end

// UIAlertViewDelegate实现代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"buttonIndex=%ld", buttonIndex);
    
    // 方法1
//    if (0 == buttonIndex)
//    {
//        NSLog(@"点击了取消按钮");
//    }
//    else if (1 == buttonIndex)
//    {
//        NSLog(@"点击了确定按钮");
//    }
    
    // 方法2
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    BOOL isEqual = [title isEqualToString:@"知道0"];
    if (isEqual)
    {
        NSLog(@"点击了确定按钮");
    }
    else
    {
        NSLog(@"点击了取消按钮");
    }
}

// 方法4 有代理,只有一个确定按钮,输入模式
UIAlertView *alertview004 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请输入信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"知道0", nil];
    
// 默认模式,无输入框
//    alertview004.alertViewStyle = UIAlertViewStyleDefault;
// 输入不可见文本的密码模式,有一个密码输入框
//    alertview004.alertViewStyle = UIAlertViewStyleSecureTextInput;
// 输入可见文本模式,有一个文本输入框
//    alertview004.alertViewStyle = UIAlertViewStylePlainTextInput;
// 输入可见文本与不可见文本模式,即登录输入模式,有两个输入框
alertview004.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    
// 第一个输入框
UITextField *nameField = [alertview004 textFieldAtIndex:0];
nameField.placeholder = @"请输入登录帐号";
// 第二个输入框
UITextField *passwordField = [alertview004 textFieldAtIndex:1];
passwordField.placeholder = @"请输入登录密码";

// 显示
[alertview004 show];

// 设置协议
@interface ViewController () <UIAlertViewDelegate>

@end

// UIAlertViewDelegate实现代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"buttonIndex=%ld", buttonIndex);
    
    // 方法1
//    if (0 == buttonIndex)
//    {
//        NSLog(@"点击了取消按钮");
//    }
//    else if (1 == buttonIndex)
//    {
//        NSLog(@"点击了确定按钮");
//    }
    
    // 方法2
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    BOOL isEqual = [title isEqualToString:@"知道0"];
    if (isEqual)
    {
        NSLog(@"点击了确定按钮");
        
        // 输入可见文本与不可见文本模式,即登录输入模式,有两个输入框
        // 第一个输入框
        UITextField *nameField = [alertView textFieldAtIndex:0];
        NSString *name = nameField.text;
        NSLog(@"name=%@",name);
        
        // 第二个输入框
        UITextField *passwordField = [alertView textFieldAtIndex:1];
        NSString *password = passwordField.text;
        NSLog(@"password=%@",password);
    }
    else
    {
        NSLog(@"点击了取消按钮");
    }
}

// 方法5
/*
iOS8以后出现了UIAlertController视图控制器,通过设置UIAlertController的style属性来控制是alertview还是actionsheet
*/
UIAlertController *alertContoller = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" preferredStyle:UIAlertControllerStyleAlert];
// 响应方法-取消
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消按钮");
}];
// 响应方法-确定
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定按钮");
}];
// 添加响应方式
[alertContoller addAction:cancelAction];
[alertContoller addAction:confirmAction];
// 显示
[self presentViewController:alertContoller animated:YES completion:nil];







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值