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 ...
随机推荐
- Android Studio 经常使用功能介绍
为了简化 Android 的开发力度,Google 决定将重点建设 Android Studio 工具.Google 会在今年年底停止支持其它集成开发环境.比方 Eclipse. Android St ...
- u3d动态加入模型
楼层一层一层的加,把模型分开,弄成prefab放到Resourse文件夹里,在代码里用Instantiate(Resources.Load("模型名字") as GameObjec ...
- 基于jQuery的input输入框下拉提示层(自动邮箱后缀名)
基于jQuery的input输入框下拉提示层,方便用户输入邮箱时的提示信息,需要的朋友可以参考下 效果图 // JavaScript Document (function($){ $.fn ...
- CENTOS 下 编译安装 tesseract-ocr 3.0.4 识别文字
网上搜集了不少资料,发现目前Tesseract转移到了Github上,因此下载方式和以前略有差别,下文是参考了诸位达人(再此表示感谢)和自己的实际操作过程形成的.(1)首先安装依赖的leptonica ...
- Python测试基础教程
Python简明教程1-为什么 Pythonhttp://www.automationqa.com/forum.php?mod=viewthread&tid=3674&fromuid= ...
- java之数组
数组概述: 1.数组可以看成是多个相同数据类型数据的组合,对这些数据的统一管理. 2.数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量. 3.数组中的元素可以是任何类型 ...
- [原]SQLite的学习系列之获取数据库版本
最先了解到SQLite是基于其作为移动客户端数据存储平台,以下是其官网(https://www.sqlite.org/)关于SQLite的一段介绍: SQLite是遵守ACID的轻型数据库引擎,它包含 ...
- windows 2008 R2 64位系统,找到Microsoft Excel 应用程序
在windows 2003 操作系统中, 1.在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务", 2.依次双击& ...
- Android 开发框架汇总
Android 开发框架汇总 时间过的真快,转眼间就要进入到16年的8月了,现在应该是三伏期间,一个字“热”.前端时间整理了一篇“JS前端框架汇总”,然后紧接着又抽时间学习了一下Android开发,在 ...
- Enjoy Android
大趋势所迫,开始学习Android, @mark一下