一直覺得使用系統這個東西寫起來特別麻煩,每次都要寫一大推東西,還是重覆的,今天抽了點時間自己重新封裝了一下,解決了自己的強迫症。。。,不多說,直接上代碼了。 1.自己定義了一個名為XBZ的UIAlertViewControllerde 分類,.h文件裡面 2.然後是.m文件 3.調用方法,就統一寫到 ...
一直覺得使用系統這個東西寫起來特別麻煩,每次都要寫一大推東西,還是重覆的,今天抽了點時間自己重新封裝了一下,解決了自己的強迫症。。。,不多說,直接上代碼了。
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 不帶message的彈出提示,預設顯示2s自動dismiss 11 12 @param controller 當前彈出的控制器 13 @param title 標題 14 */ 15 + (void)alertWithController:(nonnull UIViewController *)controller title:(nonnull NSString *)title; 16 17 18 /** 19 不帶message的彈出提示,可自定義dismiss時間 20 21 @param controller 當前彈出的控制器 22 @param title 標題 23 @param timerInerval dismiss時間 24 */ 25 + (void)alertWithController:(nonnull UIViewController *)controller title:(nonnull NSString *)title timeInterval:(NSTimeInterval)timerInerval; 26 27 /** 28 帶message的彈出提示,預設顯示2s自動dismiss掉 29 30 @param controller 當前彈出的控制器 31 @param title 標題 32 @param message 消息內容 33 */ 34 + (void)alertWithController:(nonnull UIViewController *)controller title:(nonnull NSString *)title message:(nonnull NSString *)message; 35 36 /** 37 帶message的彈出提示,可自定義dismiss時間 38 39 @param controller 當前彈出的控制器 40 @param title 標題 41 @param message 消息內容 42 @param timerInerval dismiss時間 43 */ 44 + (void)alertWithController:(nonnull UIViewController *)controller title:(nonnull NSString *)title message:(nonnull NSString *)message timeInterval:(NSTimeInterval)timerInerval; 45 46 47 /** 48 使用預設action風格的alert,最多支持兩個按鈕(一般兩個就夠了) 49 50 @param controller 當前彈出的控制器 51 @param title 標題 52 @param message 消息內容 53 @param titles 按鈕標題數組 54 @param actionOne 第一個按鈕事件 55 @param actionTwo 第二個按鈕事件 56 @param alertStyle 彈出方式,採用sheet時會自動加上cacel按鈕 57 */ 58 + (void)alertWithController:(nonnull UIViewController *)controller title:(NSString *)title message:(NSString *)message actionTitles:(nonnull NSArray<NSString *> *)titles actionOne:(ActionOne)actionOne actionTwo:(ActionTwo)actionTwo alertStyle:(UIAlertControllerStyle)alertStyle; 59 60 /** 61 可自己定義action風格的alert 62 63 @param controller 當前彈出的控制器 64 @param title 標題 65 @param message 消息內容 66 @param titles 按鈕標題數組 67 @param actionStyles action風格,數組形式 68 @param actionOne 第一個按鈕事件 69 @param actionTwo 第二個按鈕事件 70 @param alertStyle 彈出方式,採用sheet時會自動加上cacel按鈕 71 */ 72 + (void)alertWithController:(nonnull UIViewController *)controller title:(NSString *)title message:(NSString *)message actionTitles:(nonnull NSArray<NSString *> *)titles actionStyles:(NSArray<NSNumber *> *)actionStyles actionOne:(ActionOne)actionOne actionTwo:(ActionOne)actionTwo alertStyle:(UIAlertControllerStyle)alertStyle; 73 74 @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 title:(NSString *)title { 9 10 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:@"" preferredStyle:UIAlertControllerStyleAlert]; 11 12 [NSTimer scheduledTimerWithTimeInterval:kDefaultTimerInterval target:self selector:@selector(dismissAlertController:) userInfo:alertController repeats:NO]; 13 14 [controller presentViewController:alertController animated:YES completion:nil]; 15 16 } 17 18 + (void)alertWithController:(nonnull UIViewController *)controller title:(NSString *)title timeInterval:(NSTimeInterval)timerInerval { 19 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:@"" preferredStyle:UIAlertControllerStyleAlert]; 20 21 [NSTimer scheduledTimerWithTimeInterval:timerInerval target:self selector:@selector(dismissAlertController:) userInfo:alertController repeats:NO]; 22 23 [controller presentViewController:alertController animated:YES completion:nil]; 24 } 25 26 + (void)alertWithController:(nonnull UIViewController *)controller title:(NSString *)title message:(NSString *)message { 27 28 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; 29 30 [NSTimer scheduledTimerWithTimeInterval:kDefaultTimerInterval target:self selector:@selector(dismissAlertController:) userInfo:alertController repeats:NO]; 31 32 [controller presentViewController:alertController animated:YES completion:nil]; 33 34 } 35 36 37 + (void)alertWithController:(nonnull UIViewController *)controller title:(NSString *)title message:(NSString *)message timeInterval:(NSTimeInterval)timerInerval { 38 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; 39 40 [NSTimer scheduledTimerWithTimeInterval:timerInerval target:self selector:@selector(dismissAlertController:) userInfo:alertController repeats:NO]; 41 42 [controller presentViewController:alertController animated:YES completion:nil]; 43 } 44 45 + (void)alertWithController:(nonnull UIViewController *)controller title:(NSString *)title message:(NSString *)message actionTitles:(nonnull NSArray<NSString *> *)titles actionOne:(ActionOne)actionOne actionTwo:(ActionTwo)actionTwo alertStyle:(UIAlertControllerStyle)alertStyle { 46 47 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:alertStyle]; 48 49 switch (titles.count) { 50 break; 51 case 1: 52 { 53 [alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 54 if (actionOne) { 55 actionOne(); 56 } 57 }]]; 58 } 59 break; 60 case 2: 61 { 62 [alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 63 if (actionOne) { 64 actionOne(); 65 } 66 }]]; 67 [alertController addAction:[UIAlertAction actionWithTitle:titles.lastObject style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 68 if (actionTwo) { 69 actionTwo(); 70 } 71 }]]; 72 } 73 break; 74 default: 75 break; 76 } 77 78 79 if (alertStyle == UIAlertControllerStyleActionSheet) { 80 [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL]]; 81 } 82 83 [controller presentViewController:alertController animated:YES completion:nil]; 84 } 85 86 + (void)alertWithController:(nonnull UIViewController *)controller title:(NSString *)title message:(NSString *)message actionTitles:(nonnull NSArray<NSString *> *)titles actionStyles:(NSArray<NSNumber *> *)actionStyles actionOne:(ActionOne)actionOne actionTwo:(ActionOne)actionTwo alertStyle:(UIAlertControllerStyle)alertStyle { 87 88 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:alertStyle]; 89 90 switch (titles.count) { 91 break; 92 case 1: 93 { 94 UIAlertActionStyle style = actionStyles.firstObject ? [actionStyles.firstObject integerValue] : UIAlertActionStyleDefault; 95 [alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:style handler:^(UIAlertAction * _Nonnull action) { 96 if (actionOne) { 97 actionOne(); 98 } 99 }]]; 100 } 101 break; 102 case 2: 103 { 104 UIAlertActionStyle styleOne = actionStyles.firstObject ? [actionStyles.firstObject integerValue] : UIAlertActionStyleDefault; 105 UIAlertActionStyle styleTwo = actionStyles.lastObject ? [actionStyles.lastObject integerValue] : UIAlertActionStyleDefault; 106 [alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:styleOne handler:^(UIAlertAction * _Nonnull action) { 107 if (actionOne) { 108 actionOne(); 109 } 110 }]]; 111 [alertController addAction:[UIAlertAction actionWithTitle:titles.lastObject style:styleTwo handler:^(UIAlertAction * _Nonnull action) { 112 if (actionTwo) { 113 actionTwo(); 114 } 115 }]]; 116 } 117 break; 118 default: 119 break; 120 } 121 122 123 if (alertStyle == UIAlertControllerStyleActionSheet) { 124 [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL]]; 125 } 126 127 [controller presentViewController:alertController animated:YES completion:nil]; 128 129 } 130 131 + (void)dismissAlertController:(NSTimer *)timer { 132 133 UIAlertController *alertController = timer.userInfo; 134 135 [alertController dismissViewControllerAnimated:YES completion:nil]; 136 137 [timer invalidate]; 138 timer = nil; 139 } 140 141 @end
3.調用方法,就統一寫到一個方法裡面了~
1 - (void)example { 2 3 [UIAlertController alertWithController:self title:@"這是我的標題,我會自動2s消失"]; 4 5 [UIAlertController alertWithController:self title:@"這是我的標題,我會根據時間來讓我消失" timeInterval:4.f]; 6 7 [UIAlertController alertWithController:self title:@"我是帶message的,我會自動2s消失" message:@"我是message"]; 8 9 [UIAlertController alertWithController:self title:@"我是帶message的,我根據時間來讓我消失" message:@"我是message" timeInterval:3.f]; 10 11 [UIAlertController alertWithController:self title:@"我是帶按鈕的,使用預設風格的action style" message:@"我還是個消息" actionTitles:@[@"確定", @"取消"] actionOne:^{ 12 13 NSLog(@"點了確定了"); 14 15 } actionTwo:^{ 16 17 NSLog(@"點了取消了"); 18 19 } alertStyle:UIAlertControllerStyleAlert]; 20 21 [UIAlertController alertWithController:self title:@"我是帶按鈕的,能自定義action style的" message:@"我就是個消息" actionTitles:@[@"確定", @"取消"] actionStyles:@[@(UIAlertActionStyleDefault), @(UIAlertActionStyleDestructive)] actionOne:^{ 22 23 NSLog(@"點了確定了"); 24 25 } actionTwo:^{ 26 27 NSLog(@"點了取消了"); 28 29 } alertStyle:UIAlertControllerStyleAlert]; 30 31 }
目前就寫了最多兩個按鈕的情況,代碼也算簡化了不少,看著沒那麼亂了,再多的按鈕就單獨寫吧,反正用得不多。-_-
最後附上demo地址吧:https://github.com/BigKingQY/XBZAlertViewController_Demo.git
寫完才發現,名字裡面多了個view...就不改了...-_-!!