使用与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 提示框的更多相关文章

  1. 利用CSS中的:after、: before制作的边三角提示框

    小颖昨天分享了一篇参考bootstrap中的popover.js的css画消息弹框今天给大家再分享一篇使用:before和:after伪元素画消息弹框的CSS. 画出来是介个酱紫的: 有没有觉得画的萌 ...

  2. javascript-模板方法模式-提示框归一化插件

    模板方法模式笔记   父类中定义一组算法操作骨架,而将一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤   实例:弹出框归一化插件 css样式 ;width ...

  3. 使用纯CSS实现带箭头的提示框

    爱编程爱分享,原创文章,转载请注明出处,谢谢!http://www.cnblogs.com/fozero/p/6187323.html 1.全部代码 <!DOCTYPE html> < ...

  4. 纯css来实现提示框

    用js用多了,就疏忽了最基本的css了---用title属性来实现提示框.下面言归正传------如何用css实现提示框: 1.利用title属性来实现鼠标滑过某个元素时,实现提示整段内容的功能(利用 ...

  5. js弹出框、对话框、提示框、弹窗总结

    一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹出对话框并输出一段提示信息 funct ...

  6. android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

    需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果,  总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...

  7. android标题栏下面弹出提示框(一) TextView实现,带动画效果

    产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...

  8. 自定义iOS 中推送消息 提示框

    看到标题你可能会觉得奇怪 推送消息提示框不是系统自己弹出来的吗? 为什么还要自己自定义呢? 因为项目需求是这样的:最近需要做 远程推送通知 和一个客服系统 包括店铺客服和官方客服两个模块 如果有新的消 ...

  9. highCharts提示框不显示的问题

    使用HighCharts插件进行数据展示的时候,鼠标放在数据处没有提示框,或者只有头尾2个提示框,其他提示框不显示,为什么会这样? 1.查看是否使用了tooltip属性,该属性的enabled默认为t ...

随机推荐

  1. iOS开发备忘录:自定义UINavigationBar背景图片和Back按钮

    iOS项目,根据设计图,有时需要自定义UIView的UINavigationBar的背景.可以切出来一张1像素左右的背景图片,来充当UINavigationBar的背景. 可以利用Navigation ...

  2. 查看、关闭linux自启动网络服务

    1.查看 netstat --tulnp ..master  smtp 服务 2.关闭 /etc/init.d/服务 stop 停止 : start 启动 chkconfig 服务 off  关闭   ...

  3. U3D4.X版本无法安装MONODEV编辑器

    可能是由于机器无法成功安装.NET 4.0的缘故

  4. POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14698   Accept ...

  5. ionic介绍

    ionic介绍 Ionic是一个前端的框架,帮助开发者使用HTML5, CSS3和JavaScript做出原生应用. The beautiful, open source front-end fram ...

  6. netty ByteToMessageDecoder 分析

    ByteToMessageDecoder 1.socket 移除时触发,最后次读数据处理 @Override public final void handlerRemoved(ChannelHandl ...

  7. Qt4.8.6 Embedded Linux 的编译与移植

    最近买了个飞凌ok6410 的开发板,于是在其中搭建qt4.8.6运行环境.费了两三天时间,主要还是对Linux系统的生疏,在一些问题上徘徊很久,在这里做一些过程笔记.烧写ARM-Linux系统,根据 ...

  8. Kafka集群部署

    一. 关于kafka Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 这种动作(网页浏览,搜索和其他用户的行动)是在现代网络上的许多社会功能的一个关键 ...

  9. SpringMVC架构浅析

    SpringMVC概述 Spring的web框架围绕DispatcherServlet设计. DispatcherServlet的作用是将请求分发到不同的处理器. Spring的web框架包括可配置的 ...

  10. [git]解决:git config --global push.default matching

    解决:git config --global push.default matching 这个警告的意思是:需要设置默认push的分支,所以设置好全局push的默认分支就好了.命令如下: 在有git目 ...