自己封装了的AlertController
一直觉得使用系统这个东西写起来特别麻烦,每次都要写一大推东西,还是重复的,今天抽了点时间自己重新封装了一下,解决了自己的强迫症。。。,不多说,直接上代码了。
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...就不改了...-_-!!
转载于:https://www.cnblogs.com/BigKingBlog/p/9263456.html
自己封装了的AlertController相关推荐
- 从零开始学Android架构(一)——什么是设计模式?
前言 不少人会觉得架构师是一个高大上的岗位,只有技术顶尖的人才能胜任,但其实它并没有这么高大上,大部分的架构师,都只是开发经验非常丰富,并且热爱学习,善于知识迁移和总结.应用的架构是一件非常成熟,有非 ...
- iOS (封装)一句话调用系统的alertView和alertController
前言: 本文仅作参考存留,请用新版封装:iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用 UIAlertController是iOS8.0之后出来的新方法,其将 ...
- IDEA中将代码块封装为方法,IDEA代码重构快捷键
IDEA中将代码块封装为方法 选中要转换的代码块,快捷键: Windows快捷键:Alt + Shift + M Mac快捷键:Alt + Command + M 如图:
- 2022-2028年中国封装用胶膜行业运营现状及投资发展潜力报告
[报告类型]产业研究 [出版时间]即时更新(交付时间约3个工作日) [发布机构]智研瞻产业研究院 [报告格式]PDF版 本报告介绍了封装用胶膜行业相关概述.中国封装用胶膜行业运行环境.分析了中国封装用 ...
- 第十六节,使用函数封装库tf.contrib.layers
目录 一 tf.contrib.layers中的具体函数介绍 1.tf.contrib.layers.conv2d()函数的定义如下: 2.tf.contrib.layers.max_pool2d() ...
- 薄膜封装,等离子体技术,原子层沉积,化学气相沉积
薄膜封装,等离子体技术,原子层沉积,化学气相沉积 薄膜封装 薄膜封装概念 薄膜真空沉积的一个很重要的技术应用就是薄膜封装.人们对薄膜封装最简单的认识就是日常生活中最常见的保鲜膜,水氧渗透率大约是1-1 ...
- 为什么要使用Retrofit封装OkHttp,而不单独使用OkHttp?
OkHttp的优点: 开源的轻量级框架.高效.快速的请求客户端,可以单独使用它来实现网络请求. 支持SPDY: 支持连接池,可极大减少延时: 支持Gzip压缩响应体,降低传输内容的大小: 支持Http ...
- Python Day26:多态、封装、内置函数:__str__、__del__、反射(反省)、动态导入模块...
## 多态 ```python OOP中标准解释:多个不同类型对象,可以响应同一个方法,并产生不同结果,即为多态 多态好处:只要知道基类使用方法即可,不需要关心具体哪一个类的对象实现的,以不变应万变, ...
- 09_Java面向对象_第9天(类、封装)_讲义
今日内容介绍 1.面向对象思想 2.类与对象的关系 3.局部变量和成员变量的关系 4.封装思想 5.private,this关键字 6.随机点名器 01面向对象和面向过程的思想 A: 面向过程与面向对 ...
最新文章
- python面试题目
- jQuery 遍历 (each、map)
- Go语言学习之encoding/json包
- 03-对图像进行数值计算和加权融合
- μC/OS-II实时性能测试与分析
- java 常用的五大包
- 大学计算机试题在线,大学计算机基础试题选择题.pdf
- 三维激光LiDAR点云数据处理,我帮您!
- 远程服务器学习总结篇1:ssh
- 黑塞矩阵和雅克比矩阵
- 【数据分析|面试】如何介绍你的项目经历
- 谷歌命名工具_Google地图正在重命名整个社区
- 利用计算机教室教师培训记录表,新学期教师计算机培训方案
- 贝塞尔曲线-曲线拟合
- ajaxSubmit的使用总结
- 第三章 ContextCapture 19 空三处理
- Fastadmin和Easywechat
- 转载: 中国大部分程序员的通病
- 2020年flag立起来
- 基于Java毕业设计幼儿园管理系统源码+系统+mysql+lw文档+部署软件