提示框UIAlertView

这里写图片描述

UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 20, 60, 40);
    btn.backgroundColor = [UIColor grayColor];
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    UIButton* btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(0, 70, 60, 40);
    btn1.backgroundColor = [UIColor grayColor];
    [btn1 setTitle:@"按键" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btnAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];

    /*
     第一个参数是提示框的标题
     第二个参数是提示框的消息内容
     第三个参数是提示框的代理
     第四个参数是提示框的取消按钮的标题
     第五个参数是提示框其他按钮的标题(可以加好多个⚠️)
     */
    _alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您可以选择不同的颜色" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"black",@"darkGray",@"lightGray", nil];
    _alertView.tag = 1;

    //添加其他按钮
    //[alertView addButtonWithTitle:@"yelloew"];

    [self.view addSubview:_alertView];

    _alertView1 = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您可以选择不同的颜色" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"green",@"blue",@"cyan", nil];
    _alertView1.tag = 2;

    //添加其他按钮
    //[alertView addButtonWithTitle:@"yelloew"];

    [self.view addSubview:_alertView1];

调用的方法:

-(void)btnAction:(UIButton*)sender
{
    UIAlertView* alertView = [self.view viewWithTag:1];
    [alertView show];
}

-(void)btnAction1:(UIButton*)sender
{
    UIAlertView* alertView1 = [self.view viewWithTag:2];
    [alertView1 show];
}

//取消按钮的索引值固定为0,其他按钮从上往下依次是12.。。。。。。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"----%ld",buttonIndex);

    //获取被点击按钮的标题,参数是按钮的索引值
    NSString* title = [alertView buttonTitleAtIndex:buttonIndex];
    NSLog(@"====%@",title);
    //isEqualToString 判断两个字符串是否相等,返回的是BOOL的值

    //第一种方法⚠️
    /*
    if (alertView.tag == 1) {
        switch (buttonIndex) {
            case 1:
                self.view.backgroundColor = [UIColor blackColor];
                break;
            case 2:
                self.view.backgroundColor = [UIColor darkGrayColor];
                break;
            case 3:
                self.view.backgroundColor = [UIColor lightGrayColor];
                break;
            case 0://点取消键
                self.view.backgroundColor = [UIColor whiteColor];
                break;
            default:
                break;
        }
    }
    if (alertView.tag == 2) {
        switch (buttonIndex) {
            case 1:
                self.view.backgroundColor = [UIColor greenColor];
                break;
            case 2:
                self.view.backgroundColor = [UIColor blueColor];
                break;
            case 3:
                self.view.backgroundColor = [UIColor cyanColor];
                break;
            case 0://点取消键
                self.view.backgroundColor = [UIColor whiteColor];
                break;
            default:
                break;
        }
    }
    */

    //第二种方法⚠️
    //按钮
    /*
    if ([title isEqualToString:@"black"]) {
        self.view.backgroundColor = [UIColor blackColor];
    }
    if ([title isEqualToString:@"darkGray"]) {
        self.view.backgroundColor = [UIColor darkGrayColor];
    }
    if ([title isEqualToString:@"lightGray"]) {
        self.view.backgroundColor = [UIColor lightGrayColor];
    }
    if ([title isEqualToString:@"取消"]) {
        self.view.backgroundColor = [UIColor whiteColor];
    }

    //按键
    if ([title isEqualToString:@"green"]) {
        self.view.backgroundColor = [UIColor greenColor];
    }
    if ([title isEqualToString:@"blue"]) {
        self.view.backgroundColor = [UIColor blueColor];
    }
    if ([title isEqualToString:@"cyan"]) {
        self.view.backgroundColor = [UIColor cyanColor];
    }
    if ([title isEqualToString:@"取消"]) {
        self.view.backgroundColor = [UIColor whiteColor];
    }
     */

    //第三种方法
    if (alertView == _alertView) {
        switch (buttonIndex) {
            case 1:
                self.view.backgroundColor = [UIColor blackColor];
                break;
            case 2:
                self.view.backgroundColor = [UIColor darkGrayColor];
                break;
            case 3:
                self.view.backgroundColor = [UIColor lightGrayColor];
                break;
            case 0://点取消键
                self.view.backgroundColor = [UIColor whiteColor];
                break;
            default:
                break;
        }
    }
    if (alertView == _alertView1) {
        switch (buttonIndex) {
            case 1:
                self.view.backgroundColor = [UIColor greenColor];
                break;
            case 2:
                self.view.backgroundColor = [UIColor blueColor];
                break;
            case 3:
                self.view.backgroundColor = [UIColor cyanColor];
                break;
            case 0://点取消键
                self.view.backgroundColor = [UIColor whiteColor];
                break;
            default:
                break;
        }
    }



    /*
     switch (buttonIndex) {
     case 1:
     self.view.backgroundColor = [UIColor redColor];
     break;
     case 2:
     self.view.backgroundColor = [UIColor greenColor];
     break;
     case 3:
     self.view.backgroundColor = [UIColor blackColor];
     break;
     case 0:
     self.view.backgroundColor = [UIColor whiteColor];
     break;
     default:
     break;
     }
     */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值