UIAlertController分类

UIAlertController分类

此分类对UIAlertController进行封装, 可以直接在外部一行代码调用。
功能包含UIAlertController和actionSheet样式的弹框,点击相关按钮收到相应回调。

使用

  // 显示一个按钮的alert
    [UIAlertController showAlertControllerTitle:@"提示" msg:@"提示内容XXXX" btnTitle:@"知道了" callBack:^{
        // 点击的回调
    }];
    
    // 显示两个按钮的alert
    [UIAlertController showAlertControllerTitle:@"提示" msg:@"提示内容XXXXX" leftTitle:@"取消" rightTitle:@"确定" leftCallBack:^{
        // TODO: 点击取消的回调
    } rightCallBack:^{
        // TODO: 点击确定的回调
    }];
    
    
    // 显示一个按钮的actionSheet
    [UIAlertController showAlertControllerActionSheetWithTitle:@"请选择" actionArr:@[@"选择1", @"选择2", @"选择3"] callBack:^(NSString *selectedActionStr) {
        NSLog(@"选择的内容-----%@", selectedActionStr);
    }];

源码

UIAlertController+Category.h
+ (void)showAlertControllerTitle:(NSString *)title
                             msg:(NSString *)msg
                       leftTitle:(NSString *)leftTitle
                      rightTitle:(NSString *)rightTitle
                    leftCallBack:(void(^)(void))leftCallBack
                   rightCallBack:(void(^)(void))rightCallBack;



+ (void)showAlertControllerTitle:(NSString *)title
                             msg:(NSString *)msg
                        btnTitle:(NSString *)btnTitle
                        callBack:(void(^)(void))callBack;

// actionSheet
+ (void)showAlertControllerActionSheetWithTitle:(NSString *)title
                                      actionArr:(nonnull NSArray <NSString *>*)actionArr
                                       callBack:(void(^)(NSString *selectedActionStr))callBack;




+ (void)dissmissAlertVC;
UIAlertController+Category.m
+ (void)showAlertControllerTitle:(NSString *)title
                             msg:(NSString *)msg
                       leftTitle:(NSString *)leftTitle
                      rightTitle:(NSString *)rightTitle
                    leftCallBack:(void(^)(void))leftCallBack
                   rightCallBack:(void(^)(void))rightCallBack {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:(UIAlertControllerStyleAlert)];
    
    
    UIAlertAction *leftAction = [UIAlertAction actionWithTitle:leftTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        if (leftCallBack) {
            leftCallBack();
        }
    }];
    
    UIAlertAction *rightAction = [UIAlertAction actionWithTitle:rightTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        if (rightCallBack) {
            rightCallBack();
        }
    }];
    
    [alertController addAction:leftAction];
    [alertController addAction:rightAction];
    [[self getCurrentVC] presentViewController:alertController animated:YES completion:nil];
}

+ (void)showAlertControllerTitle:(NSString *)title
                             msg:(NSString *)msg
                       btnTitle:(NSString *)btnTitle
                    callBack:(void(^)(void))callBack {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *leftAction = [UIAlertAction actionWithTitle:btnTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        if (callBack) {
            callBack();
        }
    }];
    [alertController addAction:leftAction];
    [[self getCurrentVC] presentViewController:alertController animated:YES completion:nil];
}


+ (void)showAlertControllerActionSheetWithTitle:(NSString *)title
                                      actionArr:(nonnull NSArray <NSString *>*)actionArr
                                       callBack:(void(^)(NSString *selectedActionStr))callBack {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    
    for (NSString *actionStr in actionArr) {
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:actionStr style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            if (callBack) {
                callBack(actionStr);
            }
        }];
        [alertVC addAction:alertAction];
    }
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    [alertVC addAction:cancelAction];
    [[self getCurrentVC] presentViewController:alertVC animated:YES completion:nil];
}

/// 关闭alertVC
+ (void)dissmissAlertVC {
    UIViewController *presentedAlertVC = [self getCurrentVC].presentedViewController;
    if (!presentedAlertVC) return;
    if ([presentedAlertVC isKindOfClass:[UIAlertController class]]) {
        [presentedAlertVC dismissViewControllerAnimated:NO completion:nil];
    }
}


/*
 获取当前屏幕显示的控制器
 */
+ (UIViewController *)getCurrentVC
{
    UIViewController *rootViewController =[[[[UIApplication sharedApplication] delegate] window] rootViewController];
    return [self getVisibleViewControllerFrom:rootViewController];
}

+ (UIViewController *)getVisibleViewControllerFrom:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self getVisibleViewControllerFrom:[((UINavigationController *)vc) visibleViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self getVisibleViewControllerFrom:[((UITabBarController *)vc) selectedViewController]];
    } else {
        if (vc.presentedViewController) {
            return [self getVisibleViewControllerFrom:vc.presentedViewController];
        } else {
            return vc;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值