一直觉得使用系统这个东西写起来特别麻烦,每次都要写一大推东西,还是重复的,今天抽了点时间自己重新封装了一下,解决了自己的强迫症。。。,不多说,直接上代码了。

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的更多相关文章

  1. iOS (封装)一句话调用系统的alertView和alertController

    前言: 本文仅作参考存留,请用新版封装:iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用 UIAlertController是iOS8.0之后出来的新方法,其将 ...

  2. AFN3.0封装

    总结了一下AFN3.0封装,也借鉴了其他人总结的,整理如下,希望多交流,互相进步 // // XJYSessionManager.h// // Created by XJY on 16/10/17. ...

  3. iOS常用的封装方法

    做开发也有一段时间了,看了好多大神的代码,总体感觉他们写的代码简洁,好看,然而在对比下我写的代码,混乱,无序,简直不堪入目啊! 总体来说大神们的代码封装的都比较好,对一个项目要重复用到的代码他们都会封 ...

  4. UIAlertView/UIAlertController封装使用

    基于UIAlertView封装的JXTAlertView,这个是将之前写Demo时搞的一套快捷使用alertView的工具抽离整理出来的,并提供了C函数直接调用,像这样: jxt_showAlertT ...

  5. [C#] 简单的 Helper 封装 -- RegularExpressionHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. iOS开发之App间账号共享与SDK封装

    上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...

  7. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  8. 用C语言封装OC对象(耐心阅读,非常重要)

    用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...

  9. 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~

    一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...

随机推荐

  1. [转]纯js导出json到excel(支持chrome)

    转自:http://blog.csdn.net/educast/article/details/52775559 function JSONToExcelConvertor(JSONData, Fil ...

  2. Web前端面试指导(二十):JavaScript中如何翻转一个字符串?

    题目点评 字符串作在程序中是非常常见的,因为程序中绝大部分的数据都可以当作字符串来处理.需要对字符的处理方法比较熟悉,在回答的时候尽量能够说出多种解决方法更好! 字符串翻转的方法 1)使用字符串函数 ...

  3. hack (浏览器兼容css hack)

    1.hack的原理 由于不同的浏览器对CSS的支持及解析结果不一样,还由于CSS中的优先级的关系.我们就可以根据这个来针对不同的浏览器来写不同的CSS. CSS Hack大致有3种表现形式,CSS类内 ...

  4. C# 中重载自增自减操作符的具体运算原理 ----从C++程序员的角度看C#自增操作符重载的实质

    看了看C#的运算符重载,发现与C++打不相同.刚刚被C#的自增操作符坑了,现在来分享一下. 先定义一个类 class A { public int i; public A(int I) { i = I ...

  5. android测试Code

    <!--android:layout_alignParentTop="true"--><com.koooke.platform.View.CenterImage ...

  6. sqlserver2008 insert语句性能

    在sqlserver2008中“新建查询”,执行批量添加语句的执行时间: declare @i int ) begin INSERT INTO [xxx].[dbo].[北京万奇亚讯科技_QueryL ...

  7. C++ 类对象的初始化顺序 ZZ

    C++构造函数调用顺序 1.     创建派生类的对象,基类的构造函数优先被调用(也优先于派生类里的成员类): 2.    如果类里面有成员类,成员类的构造函数优先被调用:(也优先于该类本身的构造函数 ...

  8. 【Leetcode】【Medium】Sum Root to Leaf Numbers (未完成)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  9. 【源码分析】cocostudio场景编辑器的触发器逻辑

    去看场景编辑器的差不多都可以看到有模拟器的设置(菜单栏的设置).默认是选择cocostudio安装路径中的Simulator.exe这个模拟器,看官网介绍是自己可以选择模拟器,而且公开源代码可以按需设 ...

  10. hosts配置

    转自:http://www.cnblogs.com/ylemzhang/archive/2011/10/19/2217187.htm 注意: hosts文件不支持端口映射 如果指定端口参考:  //直 ...