UIAlertController 简单封装

本文介绍了如何对 UIAlertController 进行简单封装,并展示了其在实际使用中的代码示例,最终呈现了封装后的效果。

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

代码:

封装部分:

#pragma - mark - AlertController 封装
+(void)showCyAlertInController:( UIViewController *  __nullable) controller
                         Title:(NSString * __nullable)title
                       Message:(NSString * __nullable)message
             ButtonsTitleArray:(NSArray *)buttonsTitleArray
                       Handler:(void (^)(int selectedIndex))handler
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                      preferredStyle:UIAlertControllerStyleAlert ];
    //空按钮集合
    if (buttonsTitleArray.count<1) {
        return;
    }
    //分配事件
    for (int i=0; i<buttonsTitleArray.count; i++) {
        NSString *btnTitle=buttonsTitleArray[i];
        UIAlertAction *actions = [UIAlertAction actionWithTitle:btnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action){
            NSLog(@"选择了:%@",btnTitle);
            handler(i);
        }];
        [alertController addAction:actions];
    }
    //获取根控制器进行跳转弹出事件
    UIViewController *rootViewController=[UIApplication sharedApplication].keyWindow.rootViewController;
    rootViewController=controller==nil?rootViewController:controller;
    //查看根控制器是否已推出控制器,如果有,在已推出控制器消失前,无法推出新的控制器。
    UIViewController *presentedVC=rootViewController.presentedViewController;
    if (presentedVC) {
        NSLog(@"已经有一个推出的控制器:%@",[presentedVC class]);
   //        [presentedVC dismissViewControllerAnimated:YES completion:nil];
    }
    [rootViewController presentViewController:alertController animated:YES completion:nil];
    
}

使用部分:

[CyloHelper showCyAlertInController:nil
                                  Title:@"标题"
                                Message:@"你的银行卡里面的钱谁拿走的?"
                      ButtonsTitleArray:@[@"你邻居二姐",@"你哥",@"你大爷"]
                                Handler:^(int selectedIndex){
        switch (selectedIndex) {
            case 0:
                NSLog(@"弄她!");
                break;
            case 1:
                NSLog(@"弄他!");
                break;
            case 2:
                NSLog(@"弄Ta!");
                break;
                
            default:
                break;
        }
    }];

效果:
效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值