码迷,mamicode.com
首页 > 其他好文 > 详细

UIAlertController+Extention

时间:2018-11-21 16:12:49      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:nta   va_start   bre   sar   present   animate   ref   .com   nsstring   

调用:

        [UIAlertController showWithTitle:@"提示" buttonTitle:@"确定" block:^{
            NSLog(@"确定");
        }];
        [UIAlertController showWithTitle:@"提示"
                                 message:nil
                      rootViewController:nil
                       cancelButtonTitle:@"取消"
                       otherButtonTitles:@[@"确定"]
                            actionBlocks:^{
                                NSLog(@"取消");
                            }, ^{
                                NSLog(@"确定");
                            }, nil];

UIAlertController+LYFExtention.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
typedef void(^AlertBlock)(void);

@interface UIAlertController (LYFExtention)
/**
 显示UIAlertController,只有一个按钮
 @param title title
 @param buttonTitle 按钮文字
 @param block 按钮的回调
 */
+ (void)showWithTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle block:(AlertBlock)block;

/**
 显示UIAlertController
 
 @param title title
 @param message message
 @param viewController 在哪个UIViewcontroller上显示,可以为nil
 @param cancelString 取消按钮文字,为nil则没有取消按钮
 @param otherTitles 其他按钮文字
 @param block 响应按钮的block,如果cancelString不为nil,则第一个为cancel按钮的,之后的block依次对应otherTitles的元素
 */
+ (void)showWithTitle:(NSString *)title message: (nullable NSString *)message rootViewController:(nullable UIViewController *)viewController cancelButtonTitle:(nullable NSString *)cancelString otherButtonTitles:(NSArray <NSString *>*)otherTitles actionBlocks:(AlertBlock)block, ...NS_REQUIRES_NIL_TERMINATION;
@end

NS_ASSUME_NONNULL_END

UIAlertController+LYFExtention.m

#import "UIAlertController+LYFExtention.h"

@implementation UIAlertController (LYFExtention)

+ (void)showWithTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle block:(AlertBlock)block {
    [UIAlertController showWithTitle:title ?: @"" message:@"" rootViewController:nil cancelButtonTitle:nil otherButtonTitles:@[buttonTitle ?: @"确定"] actionBlocks:^{
        block();
    }, nil];
}

+ (void)showWithTitle:(NSString *)title message: (nullable NSString *)message rootViewController:(nullable UIViewController *)viewController cancelButtonTitle:(nullable NSString *)cancelString otherButtonTitles:(NSArray <NSString *>*)otherTitles actionBlocks:(AlertBlock)block, ... {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message ?: @"" preferredStyle:UIAlertControllerStyleAlert];
    int i = 0;
    if (cancelString) {
        [alert addAction:[UIAlertAction actionWithTitle:cancelString style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            block();
        }]];
    } else {
        if (otherTitles.count > 0) {
            [alert addAction:[UIAlertAction actionWithTitle:otherTitles[0] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                block();
            }]];
            i = 1;
        }
    }
    if (otherTitles.count > i) {
        va_list arg_list;
        va_start(arg_list, block);
        AlertBlock tempBlock;
        while ((tempBlock = va_arg(arg_list, AlertBlock))) {
            if (i >= otherTitles.count) {
                break;
            }
            [alert addAction:[UIAlertAction actionWithTitle:otherTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                tempBlock();
            }]];
            i++;
        }
        va_end(arg_list);
        
        for (; i < otherTitles.count; i++) {
            [alert addAction:[UIAlertAction actionWithTitle:otherTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }]];
        }
    }

    if (!viewController) {
        viewController = [alert visibleViewController:nil];
        if (!viewController) {
            return;
        }
    }

    [viewController presentViewController:alert animated:true completion:nil];
}

- (UIViewController *)visibleViewController:(UIViewController *)rootViewController {
    UIViewController *root;
    if (rootViewController) {
        root = rootViewController;
    } else {
        UIViewController *controller  = UIApplication.sharedApplication.delegate.window.rootViewController;
        if (!controller) {
            return nil;
        }
        root = controller;
    }
    UIViewController *presented = root.presentedViewController;
    if (!presented) {
        return root;
    }
    if ([presented isKindOfClass:[UINavigationController class]]) {
        return [self visibleViewController:((UINavigationController *)presented).topViewController];
    }
    if ([presented isKindOfClass:[UITabBarController class]]) {
        return [self visibleViewController:((UITabBarController *)presented).selectedViewController];
    }
    return presented;
}
@end

GitHub地址:

https://github.com/liuyongfa/UIAlertController-Extention.git

UIAlertController+Extention

标签:nta   va_start   bre   sar   present   animate   ref   .com   nsstring   

原文地址:https://www.cnblogs.com/liuyongfa/p/9994211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!