自己封装了的AlertController
一直觉得使用系统这个东西写起来特别麻烦,每次都要写一大推东西,还是重复的,今天抽了点时间自己重新封装了一下,解决了自己的强迫症。。。,不多说,直接上代码了。
1.自己定义了一个名为XBZ的UIAlertViewControllerde 分类,.h文件里面
#import <UIKit/UIKit.h> typedef void(^ActionOne)(void); typedef void(^ActionTwo)(void); @interface UIAlertController (XBZ) /**
只显示标题,默认2s后dismiss @param controller 当前弹出的控制器
@param title 标题
*/
+ (void)alertWithController:(nonnull UIViewController *)controller
title:(nonnull NSString *)title; /**
只显示标题,自定义dismiss时间 @param controller 当前弹出的控制器
@param title 标题
@param timerInerval dismiss时间
*/
+ (void)alertWithController:(nonnull UIViewController *)controller
title:(nonnull NSString *)title
timeInterval:(NSTimeInterval)timerInerval; /**
显示标题+消息,默认显示2s后dismiss @param controller 当前弹出的控制器
@param title 标题
@param message 消息内容
*/
+ (void)alertWithController:(nonnull UIViewController *)controller
title:(nonnull NSString *)title
message:(nonnull NSString *)message; /**
显示标题+消息,可自定义dismiss时间 @param controller 当前弹出的控制器
@param title 标题
@param message 消息内容
@param timerInerval dismiss时间
*/
+ (void)alertWithController:(nonnull UIViewController *)controller
title:(nonnull NSString *)title
message:(nonnull NSString *)message
timeInterval:(NSTimeInterval)timerInerval; /**
显示默认弹出和按钮风格(常用一) @param controller 当前弹出的控制器
@param title 标题
@param message 消息内容
@param titles 按钮标题数组,可只传一个,actionTwo置nil
@param actionOne 第一个按钮事件
@param actionTwo 第二个按钮事件
*/
+ (void)alertWithController:(nonnull UIViewController *)controller
title:(NSString *)title
message:(NSString *)message
actionTitles:(nonnull NSArray<NSString *> *)titles
actionOne:(ActionOne)actionOne
actionTwo:(ActionTwo)actionTwo; /**
显示默认按钮风格,可自定义弹出风格 @param controller 当前弹出的控制器
@param title 标题
@param message 消息内容
@param titles 按钮标题数组,可只传一个,actionTwo置nil
@param actionOne 第一个按钮事件
@param actionTwo 第二个按钮事件
@param isAlertStyle 弹出方式,YES为Alert,NO为Sheet,采用sheet时会自动加上cacel按钮
*/
+ (void)alertWithController:(nonnull UIViewController *)controller
title:(NSString *)title
message:(NSString *)message
actionTitles:(nonnull NSArray<NSString *> *)titles
actionOne:(ActionOne)actionOne
actionTwo:(ActionTwo)actionTwo
isAlertStyle:(BOOL)isAlertStyle; /**
显示默认弹出风格,可自定义按钮风格(常用二) @param controller 当前弹出的控制器
@param title 标题
@param message 消息内容
@param titles 按钮标题数组,可只传一个,actionTwo置nil
@param actionStyles 自定义按钮风格
@param actionOne 第一个按钮事件
@param actionTwo 第二个按钮事件
*/
+ (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; /**
自定义弹出风格和按钮风格 @param controller 当前弹出的控制器
@param title 标题
@param message 消息内容
@param titles 按钮标题数组,可只传一个,actionTwo置nil
@param actionStyles action风格,数组形式
@param actionOne 第一个按钮事件
@param actionTwo 第二个按钮事件
@param isAlertStyle 弹出方式,YES为Alert,NO为Sheet,采用sheet时会自动加上cacel按钮
*/
+ (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
isAlertStyle:(BOOL)isAlertStyle; @end
2.然后是.m文件
#import "UIAlertController+XBZ.h" NSTimeInterval kDefaultTimerInterval = .f; @implementation UIAlertController (XBZ) + (void)alertWithController:(nonnull UIViewController *)controller
title:(NSString *)title { [self alertWithController:controller title:title timeInterval:kDefaultTimerInterval]; } + (void)alertWithController:(nonnull UIViewController *)controller
title:(NSString *)title
timeInterval:(NSTimeInterval)timerInerval { [self alertWithController:controller title:title message:@"" timeInterval:timerInerval]; } + (void)alertWithController:(nonnull UIViewController *)controller
title:(NSString *)title
message:(NSString *)message { [self alertWithController:controller title:title message:message timeInterval:kDefaultTimerInterval]; } + (void)alertWithController:(nonnull UIViewController *)controller
title:(NSString *)title
message:(NSString *)message
timeInterval:(NSTimeInterval)timerInerval { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; [NSTimer scheduledTimerWithTimeInterval:timerInerval target:self selector:@selector(dismissAlertController:) userInfo:alertController repeats:NO]; [controller presentViewController:alertController animated:YES completion:nil];
} + (void)alertWithController:(nonnull UIViewController *)controller
title:(NSString *)title
message:(NSString *)message
actionTitles:(nonnull NSArray<NSString *> *)titles
actionOne:(ActionOne)actionOne
actionTwo:(ActionTwo)actionTwo { [self alertWithController:controller title:title message:message actionTitles:titles actionOne:actionOne actionTwo:actionTwo isAlertStyle:YES];
} + (void)alertWithController:(nonnull UIViewController *)controller
title:(NSString *)title
message:(NSString *)message
actionTitles:(nonnull NSArray<NSString *> *)titles
actionOne:(ActionOne)actionOne
actionTwo:(ActionTwo)actionTwo
isAlertStyle:(BOOL)isAlertStyle; { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:isAlertStyle ? UIAlertControllerStyleAlert : UIAlertControllerStyleActionSheet]; switch (titles.count) {
break;
case :
{
[alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (actionOne) {
actionOne();
}
}]];
}
break;
case :
{
[alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (actionOne) {
actionOne();
}
}]];
[alertController addAction:[UIAlertAction actionWithTitle:titles.lastObject style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (actionTwo) {
actionTwo();
}
}]];
}
break;
default:
break;
} if (!isAlertStyle) {
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL]];
} [controller presentViewController:alertController animated:YES completion:nil];
} + (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 { [self alertWithController:controller title:title message:message actionTitles:titles actionStyles:actionStyles actionOne:actionOne actionTwo:actionTwo isAlertStyle:YES]; } + (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
isAlertStyle:(BOOL)isAlertStyle { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:isAlertStyle ? UIAlertControllerStyleAlert : UIAlertControllerStyleActionSheet]; switch (titles.count) {
break;
case :
{
UIAlertActionStyle style = actionStyles.firstObject ? [actionStyles.firstObject integerValue] : UIAlertActionStyleDefault;
[alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:style handler:^(UIAlertAction * _Nonnull action) {
if (actionOne) {
actionOne();
}
}]];
}
break;
case :
{
UIAlertActionStyle styleOne = actionStyles.firstObject ? [actionStyles.firstObject integerValue] : UIAlertActionStyleDefault;
UIAlertActionStyle styleTwo = actionStyles.lastObject ? [actionStyles.lastObject integerValue] : UIAlertActionStyleDefault;
[alertController addAction:[UIAlertAction actionWithTitle:titles.firstObject style:styleOne handler:^(UIAlertAction * _Nonnull action) {
if (actionOne) {
actionOne();
}
}]];
[alertController addAction:[UIAlertAction actionWithTitle:titles.lastObject style:styleTwo handler:^(UIAlertAction * _Nonnull action) {
if (actionTwo) {
actionTwo();
}
}]];
}
break;
default:
break;
} if (!isAlertStyle) {
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL]];
} [controller presentViewController:alertController animated:YES completion:nil]; } + (void)dismissAlertController:(NSTimer *)timer { UIAlertController *alertController = timer.userInfo; [alertController dismissViewControllerAnimated:YES completion:nil]; [timer invalidate];
timer = nil;
} @end
3.调用方法~
- (IBAction)example1:(id)sender {
[UIAlertController alertWithController:self title:@"这是我的标题,我会自动2s消失"];
}
- (IBAction)example2:(id)sender {
[UIAlertController alertWithController:self title:@"这是我的标题,我会根据时间来让我消失" timeInterval:.f];
}
- (IBAction)example3:(id)sender {
[UIAlertController alertWithController:self title:@"我是带message的,我会自动2s消失" message:@"我是message"];
}
- (IBAction)example4:(id)sender {
[UIAlertController alertWithController:self title:@"我是带message的,我根据时间来让我消失" message:@"我是message" timeInterval:.f];
}
- (IBAction)example5:(id)sender {
[UIAlertController alertWithController:self title:@"我是带按钮的" message:@"我还是个消息" actionTitles:@[@"确定", @"取消"] actionOne:^{
NSLog(@"点了确定了");
} actionTwo:^{
NSLog(@"点了取消了");
} isAlertStyle:YES];
}
- (IBAction)example6:(id)sender {
[UIAlertController alertWithController:self title:@"我是能自定义action style的" message:@"我就是个消息" actionTitles:@[@"确定", @"取消"] actionStyles:@[@(UIAlertActionStyleDefault), @(UIAlertActionStyleDestructive)] actionOne:^{
NSLog(@"点了确定了");
} actionTwo:^{
NSLog(@"点了取消了");
} isAlertStyle:YES];
}
- (IBAction)example7:(id)sender {
[UIAlertController alertWithController:self title:@"我是带按钮的" message:@"我还是个消息" actionTitles:@[@"确定", @"取消"] actionOne:^{
NSLog(@"点了确定了");
} actionTwo:^{
NSLog(@"点了取消了");
} isAlertStyle:NO];
}
目前就写了最多两个按钮的情况,代码也算简化了不少,看着没那么乱了,再多的按钮就单独写吧,反正用得不多。-_-
最后附上demo地址吧:https://github.com/BigKingQY/XBZAlertViewController_Demo.git
写完才发现,名字里面多了个view...就不改了...-_-!!
自己封装了的AlertController的更多相关文章
- iOS (封装)一句话调用系统的alertView和alertController
前言: 本文仅作参考存留,请用新版封装:iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用 UIAlertController是iOS8.0之后出来的新方法,其将 ...
- AFN3.0封装
总结了一下AFN3.0封装,也借鉴了其他人总结的,整理如下,希望多交流,互相进步 // // XJYSessionManager.h// // Created by XJY on 16/10/17. ...
- iOS常用的封装方法
做开发也有一段时间了,看了好多大神的代码,总体感觉他们写的代码简洁,好看,然而在对比下我写的代码,混乱,无序,简直不堪入目啊! 总体来说大神们的代码封装的都比较好,对一个项目要重复用到的代码他们都会封 ...
- UIAlertView/UIAlertController封装使用
基于UIAlertView封装的JXTAlertView,这个是将之前写Demo时搞的一套快捷使用alertView的工具抽离整理出来的,并提供了C函数直接调用,像这样: jxt_showAlertT ...
- [C#] 简单的 Helper 封装 -- RegularExpressionHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- iOS开发之App间账号共享与SDK封装
上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...
- Ajax实现原理,代码封装
都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...
- 用C语言封装OC对象(耐心阅读,非常重要)
用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...
- 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~
一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...
随机推荐
- csharp:Convert Image to Base64 String and Base64 String to Image
/// <summary> /// 图像转成二进制数组 /// </summary> /// <param name="imageIn">< ...
- Javascript 多物体运动1
多物体运动 <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <ti ...
- Hush Framework框架配置(续) 转自《Android和PHP最佳实践》官方站
图书资源下载 Xampp 开发环境下载:http://pan.baidu.com/share/link?shareid=531771&uk=773037279 微博实例完整源码包下载:http ...
- [PE格式分析] 4.IMAGE_FILE_HEADER
源代码如下: typedef struct _IMAGE_FILE_HEADER { +04h WORD Machine; // 运行平台 +06h WORD NumberOfSections; // ...
- [PE格式分析] 2.IMAGE_DOS_HEADER
typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header WORD e_magic; // Magic number 固定为"MZ" ...
- Redis源码学习1-sds.c
https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/sds.c#L120 /* SDSLib, A C dynami ...
- AJAX原生JavaScript写法
GET方式 //创建XMLHttpRequest对象,为考虑兼容性问题,老版本的 Internet Explorer (IE5 和 IE6)使用 ActiveX 对象 var ajax = windo ...
- [转]优化IIS7.5支持10万个同时请求的配置方法
通过对IIS7的配置进行优化,调整IIS7应用池的队列长度,请求数限制,TCPIP连接数等方面,从而使WEB服务器的性能得以提升,保证WEB访问的访问流畅 通过对IIS7的配置进行优化,调整IIS7应 ...
- The FLARE On Challenge
上周才开始做这个CTF,用一周左右的时间完成了全部7道题.算是为即将到来的找工作进行热身和学习,下面记录一下遇到的问题和学到的东西,具体的解题过程就不详细描述了. challenge1 这道题用IDA ...
- 高质量C++C编程指南笔记 标签: c++笔记 2015-11-22 20:59 179人阅读 评论(0) 收藏
1. 在多重循环中,如果有可能,应当将最长的循环放在最内层,最短的循环放在最外层,以减少 CPU 跨切循环层的次数. 2. 如果循环体内存在逻辑判断,并且循环次数很大,宜将逻辑判断移到循环体的外面 ...