自己封装了的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 ...
随机推荐
- vue 数组重复,循环报错
Vue.js默认不支持往数组中加入重复的数据.可以使用track-by="$index"来实现.
- Angular-ui/bootstarp modal 主控制器与模态框控制器传值
调用模态框: $scope.open = function (size) { //这里很关键,是打开模态框的过程 var modalInstance = $uibModal.open({ animat ...
- 转动的八卦图纯css实现
这类的东西网上一搜就是大把的,看着比较空旷的博客,所以自己也来写一个. <!DOCTYPE html> <html> <head> <meta chars ...
- flex与相对定位在国内双核浏览器极速模式下的兼容性问题
在国内的浏览器中,360浏览器,QQ浏览器等绝大部分都是双核浏览器.双核浏览器即拥有IE兼容内核和非IE极速内核两个内核,分别对应兼容模式和极速模式.兼容模式时使用IE内核,极速模式采用webkit内 ...
- 查看postgre都有哪些语句占用CPU
查看占用CPU最多的几个postgresql ps aux | grep postgres | sort -n -r -k 3 | head -10 | awk '{print $2, $3}' 查看 ...
- Raft协议--中文论文介绍
本篇博客为著名的 RAFT 一致性算法论文的中文翻译,论文名为<In search of an Understandable Consensus Algorithm (Extended Vers ...
- Python语言下图像的操作方法总结
本章主要讲解 图像的读取方式.灰度化操作.图像转化为矩阵的方法 假设 strImgPath是图像的路径, img对象将图片读入到内存中 读取图像的第一种方式:skImage from skimage ...
- 【 Oral English】Pronunciation
一.英语音素 1.元音(元首,主要部分) 特点: a.无阻碍,拖很长认可辨别 b.声音响亮 2.辅音(重点,刻意练习) 特点: a.刻意阻碍 b.短促 二.汉语元素 1.音节:最小组成成分,而非元/辅 ...
- Net编译原理简单
转载:http://blog.csdn.net/sundacheng1989/article/details/20941893 首先简单说一下计算机软件运行.所谓软件运行,就是一步一步做一些事情.计算 ...
- kivy.org - Open source Python library for rapid development of applications
kivy.org - Open source Python library for rapid development of applicationsthat make use of innovati ...