iOS中 LGLAlertView 提示框的实例代码

 更新时间:2016年09月30日 11:14:33   作者:ljmaque  
这篇文章主要介绍了iOS中 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 = 0,
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(0);
}];
[alertController addAction:cancelAction];
}
if (destructiveBtnTitle.length) {
UIAlertAction * destructiveAction = [UIAlertAction actionWithTitle:destructiveBtnTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
block(1);
}];
[alertController addAction:destructiveAction];
}
if (otherBtnTitles.length) {
UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
(!cancelBtnTitle.length && !destructiveBtnTitle.length) ? block(0) : (((cancelBtnTitle.length && !destructiveBtnTitle.length) || (!cancelBtnTitle.length && destructiveBtnTitle.length)) ? block(1) : block(2));
}];
[alertController addAction:otherActions];
va_list args;
va_start(args, otherBtnTitles);
if (otherBtnTitles.length) {
NSString * otherString;
int index = 2;
(!cancelBtnTitle.length && !destructiveBtnTitle.length) ? (index = 0) : ((cancelBtnTitle.length && !destructiveBtnTitle.length) || (!cancelBtnTitle.length && destructiveBtnTitle.length) ? (index = 1) : (index = 2));
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(0);
}];
[actionController addAction:destructiveAction];
}
if (cancelBtnTitle.length) {
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
destructiveBtnTitle.length ? block(1) : block(0);
}];
[actionController addAction:cancelAction];
}
if (otherBtnTitles.length)
{
UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
(!cancelBtnTitle.length && !destructiveBtnTitle.length) ? block(0) : (((destructiveBtnTitle.length && !cancelBtnTitle.length) || (!destructiveBtnTitle.length && cancelBtnTitle.length)) ? block(1) : block(2));
}];
[actionController addAction:otherActions];
va_list args;
va_start(args, otherBtnTitles);
if (otherBtnTitles.length) {
NSString * otherString;
int index = 2;
(!cancelBtnTitle.length && !destructiveBtnTitle.length) ? (index = 0) : ((cancelBtnTitle.length && !destructiveBtnTitle.length) || (!cancelBtnTitle.length && destructiveBtnTitle.length) ? (index = 1) : (index = 2));
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

以上所述是小编给大家介绍的iOS中 LGLAlertView 提示框的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • iOS实现相册多选图片上传功能

    iOS实现相册多选图片上传功能

    这篇文章主要为大家详细介绍了iOS实现相册多选图片上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • 分享一个关于Storyboard 跳转与传值

    分享一个关于Storyboard 跳转与传值

    近日不忙,给大家分享一个关于storyboard跳转传值的相关知识,感兴趣的朋友一起看看吧
    2015-12-12
  • IOS10 远程推送适配详细介绍

    IOS10 远程推送适配详细介绍

    这篇文章主要介绍了IOS10 远程推送适配详细介绍的相关资料,iOS10推送新增了UserNotifications Framework,这里主要介绍如何实现远程推送,需要的朋友可以参考下
    2016-12-12
  • iOS开发之如何给View添加指定位置的边框线详解

    iOS开发之如何给View添加指定位置的边框线详解

    这篇文章主要给大家介绍了iOS开发之如何给View添加指定位置的边框线的相关资料,给view加边框很容易,重点是如何给指定边框加边框,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-10-10
  • iOS关键字static extern const使用示例详解

    iOS关键字static extern const使用示例详解

    这篇文章主要为大家介绍了iOS关键字static extern const使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • ios基于UICollectionView实现横向瀑布流

    ios基于UICollectionView实现横向瀑布流

    这篇文章主要为大家详细介绍了ios基于UICollectionView实现横向瀑布流,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • iOS tableView多输入框如何获取数据

    iOS tableView多输入框如何获取数据

    这篇文章主要给大家介绍了关于iOS tableView多输入框如何获取数据的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • iOS底层探索之自动释放池原理解析

    iOS底层探索之自动释放池原理解析

    这篇文章主要介绍了iOS底层探索之自动释放池,自动释放池的压栈和出栈,通过结构体的构造函数和析构函数触发,自动释放池的本质是__AtAutoreleasePool结构体,包含构造函数和析构函数,本文通过示例代码给大家介绍的非常详细,需要的朋友参考下吧
    2022-06-06
  • iOS性能优化教程之页面加载速率详解

    iOS性能优化教程之页面加载速率详解

    在软件开发领域里经常能听到这样一句话,“过早的优化是万恶之源”,不要过早优化或者过度优化。下面这篇文章主要给大家介绍了关于iOS性能优化教程之页面加载速率的相关资料,需要的朋友可以参考下
    2018-09-09
  • 解决苹果ios用js的Date()出现NaN的问题

    解决苹果ios用js的Date()出现NaN的问题

    下面小编就为大家分享一篇解决苹果ios用js的Date()出现NaN的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03

最新评论