封装UIAlertController

.h

/*
 title:             标题
 messag:            内容
 style:             弹窗类型
 actionTitles:      actitions标题数组
 actionStyles:      actions类型
 handlers:          回调
 target:            调用者
 */
+ (void)alertWithTitle:(NSString *)title
               message:(NSString *)message
                 style:(UIAlertControllerStyle)style
          actionTitles:(NSArray *)actionTitles
          actionStyles:(NSArray *)actionStyles
                target:(UIViewController *)target
              handlers:(void(^)(NSInteger index))handlers;

.m

+ (void)alertWithTitle:(NSString *)title
               message:(NSString *)message
                 style:(UIAlertControllerStyle)style
          actionTitles:(NSArray *)actionTitles
          actionStyles:(NSArray *)actionStyles
                target:(UIViewController *)target
              handlers:(void(^)(NSInteger index))handlers {
    
    NSAssert(actionTitles.count == actionStyles.count, @"actionTitles和actionStyles要保持一致");
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:style];
    
    for (int i = 0; i < actionTitles.count; i++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitles[i] style:[actionStyles[i] integerValue] handler:^(UIAlertAction * _Nonnull action) {
            
            if (handlers) {
                handlers(i);
            }
        }];
        [alertController addAction:action];
    }
    
    [target presentViewController:alertController animated:YES completion:nil];
}

使用方法

            [AlertControllerManager alertWithTitle:nil
                                           message:nil
                                             style:UIAlertControllerStyleActionSheet
                                      actionTitles:@[@"高德地图",
                                                     @"百度地图",
                                                     @"取消"]
                                      actionStyles:@[@(UIAlertActionStyleDefault),
                                                     @(UIAlertActionStyleDefault),
                                                     @(UIAlertActionStyleCancel)]
                                            target:self
                                          handlers:^(NSInteger index) {
                                              if (index == 0) {

                                              }else {

                                              }
                                              
                                          }];

你可能感兴趣的:(封装UIAlertController)