您的位置:首页 > 产品设计 > UI/UE

iOS开发笔记(3) -- UIAlertController的二次封装

2017-03-29 18:58 351 查看
最近有点忙,有20天没写博客了.现在准备做一个新项目整理一下心得,做些准备.
做iOS开发的都知道以前用的是UIAlert后来呗废弃掉了使用UIAlertController,其实本质上UIAlertController还是一个UIViewController;而且在项目开发中也经常会用到这样或者那样的弹窗试图,选择试图等等...
如果每次都进行每一个弹框的重写会很麻烦,所以我准备对系统的UIAlertController进行二次封装,一次来实现我偷懒的目的,言归正传上代码:

首先我创建一个类继承与系统的UIAlertController取名为WBAlertController,然后请无视掉自带的viewDidLoad等方法;


重写系统的初始化方法,因为系统的初始化方法为类方法(“+”方法)所以重写的当然也只能是类方法了.为了实现我偷懒的目的,我讲我所需要的所有参数统统简化;

+ (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"]) {
WBAlertController *alert = [WBAlertController 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{
WBAlertController *alert = [WBAlertController 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];
}


获取当前的VC

-(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;
}


记得.h文件中要生命方法;

#import <UIKit/UIKit.h>

@interface WBAlertController : UIAlertController

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

-(void)showWBAler;

@end


OK,到此针对UIAlertController的二次封装我就简单封装完成了,如果有更加私有的去求,就要根据具体情况去更改了,下面附上调用例子:

//注意,这个ClickAction是我写的button的点击事件;
- (void)ClickAction{
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2",@"3", nil];
WBAlertController *alert =[WBAlertController initWBAlerControllerWithTitle:@"测试" message:@"测试啊测试啊" style:@"0" titleArr:arr  alerAction:^(NSInteger index) {

//这里为点击事件的回调;
if (index == 0) {
NSLog(@"1");
}
if (index == 1) {
NSLog(@"2");
}
if (index == 2) {
NSLog(@"3");
}
}];

[alert showWBAler];

}


如果各位看官有什么完善的意见欢迎拍砖;感谢;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: