自己封装了的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 ...
随机推荐
- String Control
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...
- Python基础-map/reduce/filter
一.map Python内置函数,用法及说明如下: class map(object): """ map(func, *iterables) --> map obj ...
- 139.00.004 Git学习-远程仓库之Github
参考Github官方HelloWorld入门教程 "有了远程仓库,妈妈再也不用担心我的硬盘了."--Git点读机 本章开始介绍Git的杀手级功能之一(注意是之一,也就是后面还有之二 ...
- Android Studio设置代码风格
进入settings,然后搜索CodeStyle选择Java进入如下界面 scheme选择project
- 目前比较全的CSS重设(reset)方法总结(转)
原文地址 在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈 ...
- PHP and laravel知识点小小积累
function () use ($x, &$y){} 自从PHP5.3开始有了closure/匿名函数的概念,在这里的use关键词的作用是允许匿名函数capture到父函数scope 内存在 ...
- mongodb 3.4复制集配置
1:启动三个实例 /bin/mongod --config /home/mongodb/db27017/mongodb27017.conf /bin/mongod --config /home/mon ...
- fork: retry: Resource temporarily unavailable
用户A打开文件描述符太多,超过了该用户的限制 修改用户可以打开的文件描述符数量 1.首先,用另一个用户B登录,修改/etc/security/limit.conf * soft nofile 6553 ...
- npm安装及webpack打包小demo
node(node.js) 安装 1.先从https://segmentfault.com/a/1190000004245357网站下载x64位的安装包node-v4.8.1-linux-x64.ta ...
- July 18th 2017 Week 29th Tuesday
My heart is stronger now that you are in it. 我的心里有了你,从此变得更强大. You will no longer feel lonely if ther ...