LGLAlertView 提示框
使用与iOS8 以后,只是把系统的UIAlertController进行了封装,省的每次用的时候要写很多的代码。封装后只需要一句代码即可 , deome 地址:https://github.com/liguoliangiOS/LGLAlertView.git
上代码LGLAlertView.h:
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, LGLAlertViewActionStyle) { LGLAlertViewActionStyleDefault = , LGLAlertViewActionStyleCancel, LGLAlertViewActionStyleDestructive }; /** alertView的回调block */ typedef void (^CallBackBlock)(NSInteger btnIndex); /** alertView的回调block */ typedef void (^TextFieldCallBackBlock)(NSString * text); @interface LGLAlertView : NSObject /** * 单个或者没有按钮 不执行任何操作 只是提示总用 * @param title 提示的标题 * @param message 提示信息 * @param btnTitle 单个按钮的标题名称 * */ + (void)showAlertViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message buttonTitle:(NSString *)btnTitle buttonStyle:(LGLAlertViewActionStyle)buttonStyle; /** * 有两个或者多个按钮 确定 取消 * @param title 提示的标题 * @param message 提示信息 * @param btnTitle 单个按钮的标题名称 * @param cancelButtonTitle 取消按钮 * @param destructiveBtn destructiveBtn按钮 * @param otherButtonTitles 确定按钮 */ + (void)showAlertViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message CallBackBlock:(CallBackBlock)textBlock cancelButtonTitle:(NSString *)cancelBtnTitle destructiveButtonTitle:(NSString *)destructiveBtnTitle otherButtonTitles:(NSString *)otherBtnTitles,...NS_REQUIRES_NIL_TERMINATION; /** * 有输入框 确定 取消 (注: 这里只做了有一个输入框) * @param title 提示的标题 * @param message 提示信息 * @param btnTitle 单个按钮的标题名称 * @param cancelButtonTitle 取消按钮 * @param destructiveBtn destructiveBtn按钮 * @param otherButtonTitles 确定按钮 */ + (void)showAlertTextFieldViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message TextFeildCallBackBlock:(TextFieldCallBackBlock)block cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitle; /** * 单个或者没有按钮ActionSheet 仅仅只是提示作用 按钮没有响应事件 * @param title 提示的标题 * @param message 提示信息 * @param btnTitle 单个按钮的标题名称 * */ + (void)showAlertActionSheetViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message buttonTitle:(NSString *)btnTitle buttonStyle:(LGLAlertViewActionStyle)buttonStyle; /** * 没有按钮ActionSheet 按钮有响应事件 * @param title 提示的标题 * @param message 提示信息 * @param btnTitle 单个按钮的标题名称 * */ + (void)showAlertActionSheetWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message callbackBlock:(CallBackBlock)block destructiveButtonTitle:(NSString *)destructiveBtnTitle cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitles, ...NS_REQUIRES_NIL_TERMINATION; @end
LGLAlertView.m:文件
#import "LGLAlertView.h"
#define LGLAlertShowTime 1.0 @implementation LGLAlertView // ======================================================================== ----- AlertView start----- ================================================================================== // 单个或没有按钮 + (void)showAlertViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message buttonTitle:(NSString *)btnTitle buttonStyle:(LGLAlertViewActionStyle)buttonStyle { UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
if (btnTitle.length) {
UIAlertActionStyle actionStyle = (buttonStyle == LGLAlertViewActionStyleDefault) ? UIAlertActionStyleDefault : ((buttonStyle == LGLAlertViewActionStyleCancel) ? UIAlertActionStyleCancel : UIAlertActionStyleDestructive);
UIAlertAction * alertAction = alertAction = [UIAlertAction actionWithTitle:btnTitle style:actionStyle handler:^(UIAlertAction * _Nonnull action) {
[self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:LGLAlertShowTime];
}];;
[alertController addAction:alertAction];
[viewController presentViewController:alertController animated:YES completion:nil];
} else {
[viewController presentViewController:alertController animated:YES completion:nil];
[self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:LGLAlertShowTime];
}
} // 单个或多个按钮 + (void)showAlertViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message CallBackBlock:(CallBackBlock)block cancelButtonTitle:(NSString *)cancelBtnTitle destructiveButtonTitle:(NSString *)destructiveBtnTitle otherButtonTitles:(NSString *)otherBtnTitles,... { UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
//添加按钮
if (cancelBtnTitle.length) {
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
block();
}];
[alertController addAction:cancelAction];
}
if (destructiveBtnTitle.length) {
UIAlertAction * destructiveAction = [UIAlertAction actionWithTitle:destructiveBtnTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
block();
}];
[alertController addAction:destructiveAction];
}
if (otherBtnTitles.length) {
UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
(!cancelBtnTitle.length && !destructiveBtnTitle.length) ? block() : (((cancelBtnTitle.length && !destructiveBtnTitle.length) || (!cancelBtnTitle.length && destructiveBtnTitle.length)) ? block() : block());
}];
[alertController addAction:otherActions]; va_list args;
va_start(args, otherBtnTitles);
if (otherBtnTitles.length) {
NSString * otherString;
int index = ;
(!cancelBtnTitle.length && !destructiveBtnTitle.length) ? (index = ) : ((cancelBtnTitle.length && !destructiveBtnTitle.length) || (!cancelBtnTitle.length && destructiveBtnTitle.length) ? (index = ) : (index = ));
while ((otherString = va_arg(args, NSString*))) {
index ++ ;
UIAlertAction * otherActions = [UIAlertAction actionWithTitle:otherString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
block(index);
}];
[alertController addAction:otherActions];
}
}
va_end(args);
}
[viewController presentViewController:alertController animated:YES completion:nil]; //如果没有按钮,自动延迟消失
if (!cancelBtnTitle.length && !destructiveBtnTitle.length && !otherBtnTitles) {
//此时self指本类
[self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:LGLAlertShowTime];
}
} // 有输入框 + (void)showAlertTextFieldViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message TextFeildCallBackBlock:(TextFieldCallBackBlock)textBlock cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitle { UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
}];
if (cancelBtnTitle.length) {
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[self dismissAlertController:alertController];
}];
[alertController addAction:cancelAction];
}
if (otherBtnTitle.length) {
UIAlertAction * otherAction = [UIAlertAction actionWithTitle:otherBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
textBlock([alertController.textFields firstObject].text);
}];
[alertController addAction:otherAction];
}
[viewController presentViewController:alertController animated:YES completion:nil];
//如果没有按钮,自动延迟消失
if (!cancelBtnTitle.length && !otherBtnTitle.length) {
//此时self指本类
[self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:LGLAlertShowTime];
} }
// ======================================================================== ----- AlertView end----- ==================================================================================
#pragma mark ==== 点击事件 ======
+ (void)dismissAlertController:(UIAlertController *)alert {
[alert dismissViewControllerAnimated:YES completion:nil];
} // ======================================================================== -- ActionSheet Start -- ==================================================================================== + (void)showAlertActionSheetViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message buttonTitle:(NSString *)btnTitle buttonStyle:(LGLAlertViewActionStyle)buttonStyle { UIAlertController * actionController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
if (btnTitle.length) {
UIAlertActionStyle actionStyle = (buttonStyle == LGLAlertViewActionStyleDefault) ? UIAlertActionStyleDefault : ((buttonStyle == LGLAlertViewActionStyleCancel) ? UIAlertActionStyleCancel : UIAlertActionStyleDestructive);
UIAlertAction * alertAction = alertAction = [UIAlertAction actionWithTitle:btnTitle style:actionStyle handler:^(UIAlertAction * _Nonnull action) {
[self performSelector:@selector(dismissAlertController:) withObject:actionController afterDelay:LGLAlertShowTime];
}];;
[actionController addAction:alertAction];
[viewController presentViewController:actionController animated:YES completion:nil];
} else {
[viewController presentViewController:actionController animated:YES completion:nil];
//如果没有按钮,自动延迟消失
[self performSelector:@selector(dismissAlertController:) withObject:actionController afterDelay:LGLAlertShowTime];
}
} + (void)showAlertActionSheetWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message callbackBlock:(CallBackBlock)block destructiveButtonTitle:(NSString *)destructiveBtnTitle cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitles, ... { UIAlertController * actionController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
//添加按钮
if (destructiveBtnTitle.length) {
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveBtnTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
block();
}];
[actionController addAction:destructiveAction];
}
if (cancelBtnTitle.length) {
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
destructiveBtnTitle.length ? block() : block();
}];
[actionController addAction:cancelAction];
}
if (otherBtnTitles.length)
{
UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
(!cancelBtnTitle.length && !destructiveBtnTitle.length) ? block() : (((destructiveBtnTitle.length && !cancelBtnTitle.length) || (!destructiveBtnTitle.length && cancelBtnTitle.length)) ? block() : block());
}];
[actionController addAction:otherActions]; va_list args;
va_start(args, otherBtnTitles);
if (otherBtnTitles.length) {
NSString * otherString;
int index = ;
(!cancelBtnTitle.length && !destructiveBtnTitle.length) ? (index = ) : ((cancelBtnTitle.length && !destructiveBtnTitle.length) || (!cancelBtnTitle.length && destructiveBtnTitle.length) ? (index = ) : (index = ));
while ((otherString = va_arg(args, NSString*))) {
index ++ ;
UIAlertAction * otherActions = [UIAlertAction actionWithTitle:otherString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
block(index);
}];
[actionController addAction:otherActions];
}
}
va_end(args);
}
[viewController presentViewController:actionController animated:YES completion:nil]; //如果没有按钮,自动延迟消失
if (!cancelBtnTitle.length && !destructiveBtnTitle.length && !otherBtnTitles.length) {
//此时self指本类
[self performSelector:@selector(dismissAlertController:) withObject:actionController afterDelay:LGLAlertShowTime];
} }
@end
LGLAlertView 提示框的更多相关文章
- 利用CSS中的:after、: before制作的边三角提示框
小颖昨天分享了一篇参考bootstrap中的popover.js的css画消息弹框今天给大家再分享一篇使用:before和:after伪元素画消息弹框的CSS. 画出来是介个酱紫的: 有没有觉得画的萌 ...
- javascript-模板方法模式-提示框归一化插件
模板方法模式笔记 父类中定义一组算法操作骨架,而将一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤 实例:弹出框归一化插件 css样式 ;width ...
- 使用纯CSS实现带箭头的提示框
爱编程爱分享,原创文章,转载请注明出处,谢谢!http://www.cnblogs.com/fozero/p/6187323.html 1.全部代码 <!DOCTYPE html> < ...
- 纯css来实现提示框
用js用多了,就疏忽了最基本的css了---用title属性来实现提示框.下面言归正传------如何用css实现提示框: 1.利用title属性来实现鼠标滑过某个元素时,实现提示整段内容的功能(利用 ...
- js弹出框、对话框、提示框、弹窗总结
一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹出对话框并输出一段提示信息 funct ...
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果, 总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...
- 自定义iOS 中推送消息 提示框
看到标题你可能会觉得奇怪 推送消息提示框不是系统自己弹出来的吗? 为什么还要自己自定义呢? 因为项目需求是这样的:最近需要做 远程推送通知 和一个客服系统 包括店铺客服和官方客服两个模块 如果有新的消 ...
- highCharts提示框不显示的问题
使用HighCharts插件进行数据展示的时候,鼠标放在数据处没有提示框,或者只有头尾2个提示框,其他提示框不显示,为什么会这样? 1.查看是否使用了tooltip属性,该属性的enabled默认为t ...
随机推荐
- C primer plus 练习题 第五章
1. #include <stdio.h> #define MINU 60 int main() { int minute, hour, m; printf("请输入分钟:&qu ...
- Adobe flash player更新失败
- js apply/call/caller/callee/bind使用方法与区别分析
一.call 方法 调用一个对象的一个方法,以另一个对象替换当前对象(其实就是更改对象的内部指针,即改变对象的this指向的内容). Js代码 call([thisObj[,arg1[, arg2[, ...
- HTML5[3]:中文换行
保证中文每行第一个字,不会出现标点符号 p { white-space: pre-wrap; }
- 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...
- Asp.net Mvc4 使用Cas单点登录
因项目需要,使用了耶鲁大学的Cas单点登录方案,在java中使用一直正常,但是在.Net中碰到了循环重定向的问题,反复测试后,总算解决了,最终的配置如下: <?xml version=" ...
- android xmlns:tools用法
一开始不明白,后来删掉这个属性之后发现会出现一个提示: pick preview layout from the "Fragment Layout" context menu 原来 ...
- mount分区为读写属性
对于只读文件系统, 如果想要挂载为可读写的, 需要重新mount下, 如将config分区mount为读写的分区: mount -o remount,rw /config
- 红黑树(二)之 C语言的实现
概要 红黑树在日常的使用中比较常用,例如Java的TreeMap和TreeSet,C++的STL,以及Linux内核中都有用到.之前写过一篇文章专门介绍红黑树的理论知识,本文将给出红黑数的C语言的实现 ...
- Android 学习笔记之Volley(六)实现获取服务器的字符串响应...
学习内容: 1.使用StringRequest实现获取服务器的字符串响应... 前几篇一直都在对服务——响应过程的源码进行分析,解析了整个过程,那么Volley中到底实现了哪些请求才是我们在开发中 ...