UIAlertController简单封装使用

提示:文章只是用来记录本人自己在学习过程中所遇到的一些问题的解决方案,如果有什么意见可以留言提出来,不喜勿喷哦!

注:本文转自 “iOS开发笔记(3) -- UIAlertController的二次封装” 如要查看原文,请点击进入。

UIAlertController简单封装使用_第1张图片
时间有脚

在我们的项目中,在需要使用到 UIAlertController 时候,不可能每次都去重新把初始化之类的东西重新写一次,还不如自己进行二次封装成工具类,直接调用工具类,这样代码的结构就更加合理。

  • AlertToolViewController.h
#import 

@interface AlertToolViewController : UIAlertController

+ (instancetype)initWBAlerControllerWithTitle:(NSString *)title message:(NSString *)message style:(NSString *)style titleArr:(NSMutableArray *)titleArr alerAction:(void (^)(NSInteger index))alerAction;

-(void)showWBAler;
@end
  • AlertToolViewController.m

#import "AlertToolViewController.h"

@interface AlertToolViewController ()

@end

@implementation AlertToolViewController

//重写方法
+ (instancetype)initWBAlerControllerWithTitle:(NSString *)title message:(NSString *)message style:(NSString *)style titleArr:(NSMutableArray *)titleArr alerAction:(void (^)(NSInteger index))alerAction{
    //参数说明:
    //title表示弹框的标题;
    //message表示弹框的展示的信息;
    //style是0或者1;代表弹框的类型;UIAlertControllerStyleActionSheet = 0,UIAlertControllerStyleAlert = 1;
    //titleArr为弹框中出现的按钮标题的数组;个数你自己决定;
    //alerAction为block回调事件,因为这里只需要判断点击的按钮坐标就可以,其他需要壳自行添加参数;
    
    //判断弹框类型
    if ([style isEqualToString:@"1"]) {
        AlertToolViewController *alert = [AlertToolViewController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        for (NSInteger i = 0; i < titleArr.count; i++) {
            UIAlertAction *confirm = [UIAlertAction actionWithTitle:[titleArr objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                if (alerAction) {
                    alerAction(i);
                }
                
            }];
            [alert addAction:confirm];
        }
        return alert;
        
    }else{
        AlertToolViewController *alert = [AlertToolViewController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
        for (NSInteger i = 0; i < titleArr.count; i++) {
            UIAlertAction *confirm = [UIAlertAction actionWithTitle:[titleArr objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                if (alerAction) {
                    alerAction(i);
                }
                
            }];
            [alert addAction:confirm];
        }
        
        return alert;
    }
    
}

-(void)showWBAler{
    [[self getCurrentVC] presentViewController:self animated:YES completion:nil];
}


/**
 得到当前控制视图

 @return 当前控制视图
 */
-(UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tempWindow in windows)
        {
            if (tempWindow.windowLevel == UIWindowLevelNormal)
            {
                window = tempWindow;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    return result;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
  • 调用
- (void)action{
    //参数说明:
    //style是0或者1;代表弹框的类型;UIAlertControllerStyleActionSheet = 0,UIAlertControllerStyleAlert = 1;
    //titleArr为弹框中出现的按钮标题的数组;个数你自己决定;
    //alerAction为block回调事件,因为这里只需要判断点击的按钮坐标就可以,其他需要壳自行添加参数;
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"确定",@"取消", nil];
    AlertToolViewController *alert =[AlertToolViewController initWBAlerControllerWithTitle:@"Title" message:@"测试啊测试啊" style:@"1" titleArr:arr  alerAction:^(NSInteger index) {
        
        //这里为点击事件的回调;
        if (index == 0) {
            NSLog(@"1");
        }
        if (index == 1) {
            NSLog(@"2");
        }
        if (index == 2) {
            NSLog(@"3");
        }
    }];
    
    [alert showWBAler];
}

你可能感兴趣的:(UIAlertController简单封装使用)