一直觉得使用系统这个东西写起来特别麻烦,每次都要写一大推东西,还是重复的,今天抽了点时间自己重新封装了一下,解决了自己的强迫症。。。,不多说,直接上代码了。
1.自己定义了一个名为XBZ的UIAlertViewControllerde 分类,.h文件里面
1 #import <UIKit/UIKit.h> 2 3 typedef void(^ActionOne)(void); 4 5 typedef void(^ActionTwo)(void); 6 7 @interface UIAlertController (XBZ) 8 9 /** 10 只显示标题,默认2s后dismiss 11 12 @param controller 当前弹出的控制器 13 @param title 标题 14 */ 15 + (void)alertWithController:(nonnull UIViewController *)controller 16 title:(nonnull NSString *)title; 17 18 19 /** 20 只显示标题,自定义dismiss时间 21 22 @param controller 当前弹出的控制器 23 @param title 标题 24 @param timerInerval dismiss时间 25 */ 26 + (void)alertWithController:(nonnull UIViewController *)controller 27 title:(nonnull NSString *)title 28 timeInterval:(NSTimeInterval)timerInerval; 29 30 /** 31 显示标题+消息,默认显示2s后dismiss 32 33 @param controller 当前弹出的控制器 34 @param title 标题 35 @param message 消息内容 36 */ 37 + (void)alertWithController:(nonnull UIViewController *)controller 38 title:(nonnull NSString *)title 39 message:(nonnull NSString *)message; 40 41 /** 42 显示标题+消息,可自定义dismiss时间 43 44 @param controller 当前弹出的控制器 45 @param title 标题 46 @param message 消息内容 47 @param timerInerval dismiss时间 48 */ 49 + (void)alertWithController:(nonnull UIViewController *)controller 50 title:(nonnull NSString *)title 51 message:(nonnull NSString *)message 52 timeInterval:(NSTimeInterval)timerInerval; 53 54 55 56 /** 57 显示默认弹出和按钮风格(常用一) 58 59 @param controller 当前弹出的控制器 60 @param title 标题 61 @param message 消息内容 62 @param titles 按钮标题数组,可只传一个,actionTwo置nil 63 @param actionOne 第一个按钮事件 64 @param actionTwo 第二个按钮事件 65 */ 66 + (void)alertWithController:(nonnull UIViewController *)controller 67 title:(NSString *)title 68 message:(NSString *)message 69 actionTitles:(nonnull NSArray<NSString *> *)titles 70 actionOne:(ActionOne)actionOne 71 actionTwo:(ActionTwo)actionTwo; 72 73 /** 74 显示默认按钮风格,可自定义弹出风格 75 76 @param controller 当前弹出的控制器 77 @param title 标题 78 @param message 消息内容 79 @param titles 按钮标题数组,可只传一个,actionTwo置nil 80 @param actionOne 第一个按钮事件 81 @param actionTwo 第二个按钮事件 82 @param isAlertStyle 弹出方式,YES为Alert,NO为Sheet,采用sheet时会自动加上cacel按钮 83 */ 84 + (void)alertWithController:(nonnull UIViewController *)controller 85 title:(NSString *)title 86 message:(NSString *)message 87 actionTitles:(nonnull NSArray<NSString *> *)titles 88 actionOne:(ActionOne)actionOne 89 actionTwo:(ActionTwo)actionTwo 90 isAlertStyle:(BOOL)isAlertStyle; 91 92 93 /** 94 显示默认弹出风格,可自定义按钮风格(常用二) 95 96 @param controller 当前弹出的控制器 97 @param title 标题 98 @param message 消息内容 99 @param titles 按钮标题数组,可只传一个,actionTwo置nil 100 @param actionStyles 自定义按钮风格 101 @param actionOne 第一个按钮事件 102 @param actionTwo 第二个按钮事件 103 */ 104 + (void)alertWithController:(nonnull UIViewController *)controller 105 title:(NSString *)title 106 message:(NSString *)message 107 actionTitles:(nonnull NSArray<NSString *> *)titles 108 actionStyles:(NSArray<NSNumber *> *)actionStyles 109 actionOne:(ActionOne)actionOne 110 actionTwo:(ActionOne)actionTwo; 111 112 /** 113 自定义弹出风格和按钮风格 114 115 @param controller 当前弹出的控制器 116 @param title 标题 117 @param message 消息内容 118 @param titles 按钮标题数组,可只传一个,actionTwo置nil 119 @param actionStyles action风格,数组形式 120 @param actionOne 第一个按钮事件 121 @param actionTwo 第二个按钮事件 122 @param isAlertStyle 弹出方式,YES为Alert,NO为Sheet,采用sheet时会自动加上cacel按钮 123 */ 124 + (void)alertWithController:(nonnull UIViewController *)controller 125 title:(NSString *)title 126 message:(NSString *)message 127 actionTitles:(nonnull NSArray<NSString *> *)titles 128 actionStyles:(NSArray<NSNumber *> *)actionStyles 129 actionOne:(ActionOne)actionOne 130 actionTwo:(ActionOne)actionTwo 131 isAlertStyle:(BOOL)isAlertStyle; 132 133 @end
2.然后是.m文件
1 #import "UIAlertController+XBZ.h" 2 3 NSTimeInterval kDefaultTimerInterval = 2.f; 4 5 @implementation UIAlertController (XBZ) 6 7 8 + (void)alertWithController:(nonnull UIViewController *)controller 9 title:(NSString *)title { 10 11 [self alertWithController:controller title:title timeInterval:kDefaultTimerInterval]; 12 13 } 14 15 + (void)alertWithController:(nonnull UIViewController *)controller 16 title:(NSString *)title 17 timeInterval:(NSTimeInterval)timerInerval { 18 19 [self alertWithController:controller title:title message:@"" timeInterval:timerInerval]; 20 21 } 22 23 + (void)alertWithController:(nonnull UIViewController *)controller 24 title:(NSString *)title 25 message:(NSString *)message { 26 27 [self alertWithController:controller title:title message:message timeInterval:kDefaultTimerInterval]; 28 29 } 30 31 32 + (void)alertWithController:(nonnull UIViewController *)controller 33 title:(NSString *)title 34 message:(NSString *)message 35 timeInterval:(NSTimeInterval)timerInerval { 36 37 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; 38 39 [NSTimer scheduledTimerWithTimeInterval:timerInerval target:self selector:@selector(dismissAlertController:) userInfo:alertController repeats:NO]; 40 41 [controller presentViewController:alertController animated:YES completion:nil]; 42 } 43 44 + (void)alertWithController:(nonnull UIViewController *)controller 45 title:(NSString *)title 46 message:(NSString *)message 47 actionTitles:(nonnull NSArray<NSString *> *)titles 48 actionOne:(ActionOne)actionOne 49 actionTwo:(ActionTwo)actionTwo { 50 51 [self alertWithController:controller title:title message:message actionTitles:titles actionOne:actionOne actionTwo:actionTwo isAlertStyle:YES]; 52 } 53 54 + (void)alertWithController:(nonnull UIViewController *)controller 55 title:(NSString *)title 56 message:(NSString *)message 57 actionTitles:(nonnull NSArray<NSString *> *)titles 58 actionOne:(ActionOne)actionOne 59 actionTwo:(ActionTwo)actionTwo 60 isAlertStyle:(BOOL)isAlertStyle; { 61 62 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:isAlertStyle ? UIAlertControllerStyleAlert : UIAlertControllerStyleActionSheet]; 63 64 switch (titles.count) { 65 break; 66 case 1: 67 { 68 [alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 69 if (actionOne) { 70 actionOne(); 71 } 72 }]]; 73 } 74 break; 75 case 2: 76 { 77 [alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 78 if (actionOne) { 79 actionOne(); 80 } 81 }]]; 82 [alertController addAction:[UIAlertAction actionWithTitle:titles.lastObject style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 83 if (actionTwo) { 84 actionTwo(); 85 } 86 }]]; 87 } 88 break; 89 default: 90 break; 91 } 92 93 94 if (!isAlertStyle) { 95 [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL]]; 96 } 97 98 [controller presentViewController:alertController animated:YES completion:nil]; 99 } 100 101 102 + (void)alertWithController:(nonnull UIViewController *)controller 103 title:(NSString *)title 104 message:(NSString *)message 105 actionTitles:(nonnull NSArray<NSString *> *)titles 106 actionStyles:(NSArray<NSNumber *> *)actionStyles 107 actionOne:(ActionOne)actionOne 108 actionTwo:(ActionOne)actionTwo { 109 110 [self alertWithController:controller title:title message:message actionTitles:titles actionStyles:actionStyles actionOne:actionOne actionTwo:actionTwo isAlertStyle:YES]; 111 112 } 113 114 + (void)alertWithController:(nonnull UIViewController *)controller 115 title:(NSString *)title 116 message:(NSString *)message 117 actionTitles:(nonnull NSArray<NSString *> *)titles 118 actionStyles:(NSArray<NSNumber *> *)actionStyles 119 actionOne:(ActionOne)actionOne 120 actionTwo:(ActionOne)actionTwo 121 isAlertStyle:(BOOL)isAlertStyle { 122 123 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:isAlertStyle ? UIAlertControllerStyleAlert : UIAlertControllerStyleActionSheet]; 124 125 switch (titles.count) { 126 break; 127 case 1: 128 { 129 UIAlertActionStyle style = actionStyles.firstObject ? [actionStyles.firstObject integerValue] : UIAlertActionStyleDefault; 130 [alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:style handler:^(UIAlertAction * _Nonnull action) { 131 if (actionOne) { 132 actionOne(); 133 } 134 }]]; 135 } 136 break; 137 case 2: 138 { 139 UIAlertActionStyle styleOne = actionStyles.firstObject ? [actionStyles.firstObject integerValue] : UIAlertActionStyleDefault; 140 UIAlertActionStyle styleTwo = actionStyles.lastObject ? [actionStyles.lastObject integerValue] : UIAlertActionStyleDefault; 141 [alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:styleOne handler:^(UIAlertAction * _Nonnull action) { 142 if (actionOne) { 143 actionOne(); 144 } 145 }]]; 146 [alertController addAction:[UIAlertAction actionWithTitle:titles.lastObject style:styleTwo handler:^(UIAlertAction * _Nonnull action) { 147 if (actionTwo) { 148 actionTwo(); 149 } 150 }]]; 151 } 152 break; 153 default: 154 break; 155 } 156 157 158 if (!isAlertStyle) { 159 [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL]]; 160 } 161 162 [controller presentViewController:alertController animated:YES completion:nil]; 163 164 } 165 166 + (void)dismissAlertController:(NSTimer *)timer { 167 168 UIAlertController *alertController = timer.userInfo; 169 170 [alertController dismissViewControllerAnimated:YES completion:nil]; 171 172 [timer invalidate]; 173 timer = nil; 174 } 175 176 @end
3.调用方法~
1 - (IBAction)example1:(id)sender { 2 3 [UIAlertController alertWithController:self title:@"这是我的标题,我会自动2s消失"]; 4 5 } 6 7 - (IBAction)example2:(id)sender { 8 9 [UIAlertController alertWithController:self title:@"这是我的标题,我会根据时间来让我消失" timeInterval:4.f]; 10 11 } 12 - (IBAction)example3:(id)sender { 13 14 [UIAlertController alertWithController:self title:@"我是带message的,我会自动2s消失" message:@"我是message"]; 15 16 } 17 - (IBAction)example4:(id)sender { 18 19 [UIAlertController alertWithController:self title:@"我是带message的,我根据时间来让我消失" message:@"我是message" timeInterval:3.f]; 20 21 } 22 - (IBAction)example5:(id)sender { 23 24 [UIAlertController alertWithController:self title:@"我是带按钮的" message:@"我还是个消息" actionTitles:@[@"确定", @"取消"] actionOne:^{ 25 26 NSLog(@"点了确定了"); 27 28 } actionTwo:^{ 29 30 NSLog(@"点了取消了"); 31 32 } isAlertStyle:YES]; 33 } 41 - (IBAction)example6:(id)sender { 42 43 [UIAlertController alertWithController:self title:@"我是能自定义action style的" message:@"我就是个消息" actionTitles:@[@"确定", @"取消"] actionStyles:@[@(UIAlertActionStyleDefault), @(UIAlertActionStyleDestructive)] actionOne:^{ 44 45 NSLog(@"点了确定了"); 46 47 } actionTwo:^{ 48 49 NSLog(@"点了取消了"); 50 51 } isAlertStyle:YES]; 52 53 } 54 55 - (IBAction)example7:(id)sender { 56 57 [UIAlertController alertWithController:self title:@"我是带按钮的" message:@"我还是个消息" actionTitles:@[@"确定", @"取消"] actionOne:^{ 58 59 NSLog(@"点了确定了"); 60 61 } actionTwo:^{ 62 63 NSLog(@"点了取消了"); 64 65 } isAlertStyle:NO]; 66 67 }
目前就写了最多两个按钮的情况,代码也算简化了不少,看着没那么乱了,再多的按钮就单独写吧,反正用得不多。-_-
最后附上demo地址吧:https://github.com/BigKingQY/XBZAlertViewController_Demo.git
写完才发现,名字里面多了个view...就不改了...-_-!!