提示框(新版)

本文介绍如何在 iOS 应用中使用 UIAlertController 创建弹出提示框,包括两种样式:UIAlertControllerStyleAlert 和 UIAlertControllerStyleActionSheet。通过按钮触发弹出框,并设置不同样式的操作按钮及其响应事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.UIAlertView(需要按钮触发)
    新建按钮

    UIButton *btn = [[UIButton allocinitWithFrame:CGRectMake(5020020050)];

    btn.backgroundColor = [UIColor redColor];

    [self.view addSubview:btn];

  为按钮添加触发事件

    [btn addTarget:self action:@selector(youTouchMe) forControlEvents:UIControlEventTouchUpInside];

  触发方法实现

-(void)youTouchMe

{

    NSLog(@"别摸我);

    新建提示框

        UIAlertController *control = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"是否购买" preferredStyle:UIAlertControllerStyleAlert];

    提示框选项1

    UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"买了");

    }];

    提示框选项2

    UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"不买" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"不买,没钱");

    }];

    添加到提示框

    [control addAction:act1];

    [control addAction:act2];

    展现

    [self presentViewController:control animated:YES completion:^{

        NSLog(@"弹出来了");

    }];

}


一.UIActionSheet(需要按钮触发)(实现过程同上,只需该按钮style)
新建按钮

    UIButton *btn = [[UIButton allocinitWithFrame:CGRectMake(5020020050)];

    btn.backgroundColor = [UIColor redColor];

    [self.view addSubview:btn];

  为按钮添加触发事件

    [btn addTarget:self action:@selector(youTouchMe) forControlEvents:UIControlEventTouchUpInside];

  触发方法实现

-(void)youTouchMe

{

    NSLog(@"别摸我);

    新建提示框

        UIAlertController *control = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"是否购买" preferredStyle:UIAlertControllerStyleActionSheet];

    提示框选项1

    UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"买了");

    }];

    提示框选项2

    UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"不买" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"不买,没钱");

    }];

    提示框3(红色选项)

    UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"先付款" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"先给定金");

    }];

    添加到提示框

    [control addAction:act1];

    [control addAction:act2];

    [control addAction:act3

    展现

    [self presentViewController:control animated:YES completion:^{

        NSLog(@"弹出来了");

    }];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值