提示框UIAlertView

来源:互联网 发布:免费的收银软件 编辑:程序博客网 时间:2024/05/16 12:05

这里写图片描述

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;     }     */
阅读全文
0 0
原创粉丝点击