自己封装了的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 ...
随机推荐
- 20个实用javascript技巧及实践(二)
21. 使用逻辑AND/OR来处理条件语句 var foo =10; foo ==10&& doSomething();// is the same thing as if (foo ...
- 牛顿迭代,多项式求逆,除法,开方,exp,ln,求幂
牛顿迭代 若 \[G(F_0(x))\equiv 0(mod\ x^{2^t})\] 牛顿迭代 \[F(x)\equiv F_0(x)-\frac{G(F_0(x))}{G'(F_0(x))}(mod ...
- Python入门-迭代器
在说迭代器之前,首先来简单说一下函数名的运用以及闭包的概念和应用,有助于我们理解以后的知识. 一.函数名的运用 函数名是一个变量,但它是一个特殊的变量,与括号配合可以执行函数的变量. 1.函数名的内存 ...
- js表单快速取值/赋值 快速生成下拉框
1.表单取值/赋值公共方法 //表单序列化:文本框的name字段和数据源一致<form id="myForm" onsubmit="return false;&qu ...
- 01_微信小程序支付
[支付流程] 1.小程序内调用登录接口,获取到用户的openid(我们这一步骤让前端去获取) 2.服务端代码这边生成订单 3.服务端调用支付统一下单的api 4.服务端将再次签名,返回5个参数(前端得 ...
- Windows Server 2008 R2配置JSP网站无法访问
在Windows Server 2008 R2中配置好JSP网站后,在本机可以使用 localhost访问网站,但是局域网内其机器无法访问,则需要在Windows Server 2008 R2的系统管 ...
- How to prepare system design questions in a tech interview?
http://blog.baozitraining.org/2014/09/how-to-prepare-system-design-questions.html 如何准备面试中的系统设计问题一直都是 ...
- javascript 时间日期处理相加,减操作方法js
javascript 时间日期处理相加,减操作方法js function dateAddDays(dataStr,dayCount){ var strdate = dataStr; // 2017年0 ...
- [问题记录]libpomelo的安装
1. 描述: 按照github上的操作完成 Windows in your libpomelo project root directory open git bash and type in mkd ...
- 安装和测试Kafka
本文主要介绍如何在单节点上安装 Kafka 并测试 broker.producer 和 consumer 功能. 下载 进入下载页面:http://kafka.apache.org/downloads ...