码迷,mamicode.com
首页 > 其他好文 > 详细

自己封装了的AlertController

时间:2018-07-04 16:02:15      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:targe   big   标题   ati   nsnumber   handler   事件   end   asto   

一直觉得使用系统这个东西写起来特别麻烦,每次都要写一大推东西,还是重复的,今天抽了点时间自己重新封装了一下,解决了自己的强迫症。。。,不多说,直接上代码了。

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...就不改了...-_-!!

自己封装了的AlertController

标签:targe   big   标题   ati   nsnumber   handler   事件   end   asto   

原文地址:https://www.cnblogs.com/BigKingBlog/p/9263456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!