iOS中 LGLAlertView 提示框的实例代码_IOS

使用与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
, 提示框
lglalertview
ios alertview、webview alert不显示、ios 自定义alertview、swift alertview、wkwebview alert失效,以便于您获取更多的相关知识。

时间: 2024-09-10 10:22:21

iOS中 LGLAlertView 提示框的实例代码_IOS的相关文章

iOS中searchBar(搜索框)光标初始位置后移_IOS

废话不多说了,直接给大家贴关键代码了,具体代码如下所示: #import <UIKit/UIKit.h> @interface SearchBar : UITextField @property (nonatomic,strong) UIButton *button; + (instancetype)searchBar; @end #import "SearchBar.h" @implementation SearchBar - (id)initWithFrame:(CGR

IOS 仿时光网选票UI实例代码_IOS

一.项目简介 该项目利用UIScrollView的各种滚动事件的监听,仿造时光网选择电影票的UI而开发的一个自定义View.使用简单,可扩展性很强.具备点击每个Item进行选票功能,选票居中功能,滑动时自动选择距离中间最近的View处于选中状态,而且对于滑动时松开手的时候是否有初始速度进行了区分处理.案例演示如下:<br/> 仿时光网选票UI 二.项目讲解 1.初始化UIScrollView中每个Item的View,把每个View放到_viewArray数组中,方便接下来的定位和管理.每一个V

PHP 实现类似js中alert() 提示框_php实例

主要应用于添加判断提示,跳转,返回,刷新. 复制代码 代码如下: /**  * JS提示跳转  * @param  $tip  弹窗口提示信息(为空没有提示)  * @param  $type 设置类型 close = 关闭 ,back=返回 ,refresh=提示重载,jump提示并跳转url  * @param  $url  跳转url  */ function alert($tip = "", $type = "", $url = "")

IOS程序开发之禁止输入表情符号实例代码_IOS

废话不多说了,先给大家展示效果图. 一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UITextViewDelegate> @end RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional

IOS 指纹识别详解及实例代码_IOS

IOS 指纹识别,这里整理下项目中用的知识. IOS 指纹识别现在,在要求安全与效率兼得的时候,普通密码已不能满足我们的要求,指纹识别就这样诞生了. 每个人都有自己专属的指纹,在需要支付等输入密码的地方,我们只需轻轻一按即可,避免了输入密码的繁琐步骤,更加安全,而且妈妈再也不用担心我们忘记密码. 其实,听着高大上,实现起来特别简单,因为苹果已经帮我们封装好了,我们只需要简单的调用就好了. 1.首先,我们需要导入头文件: #import <LocalAuthentication/LocalAuth

iOS自定义alertView提示框实例分享_IOS

本文实例为大家分享iOS自定义alertView提示框,先上图,弹框的背景色,按钮背景色,提示的消息的字体颜色都可以改变 利用单例实现丰富的自定义接口 // // PBAlertController.h // PBAlertDemo // // Created by 裴波波 on 16/4/20. // Copyright 2016年 裴波波. All rights reserved. // #import <UIKit/UIKit.h> typedef void(^PBBlock)(); @

Android 改变图标原有颜色和搜索框的实例代码

图标改变颜色:Drawable的变色,让Android也能有iOS那么方便的图片色调转换,就像同一个图标,但是有多个地方使用,并且颜色不一样,就可以用这个方法了. 搜索框: 一般是EditText实现,本文 实现 TextView图片和文字居中,键盘搜索. 来看看效果图: 图标改变颜色:第一个界面的左边(二维码)和右边(更多)两个实现,我放进去的图片是黑色的,显示出来是白色的. 搜索框:第一个界面的图片和文字居中,还可以设置间距,第二个见面搜索设置键盘搜索按钮,点击搜索监听事件,清除内容的图标.

jQuery表单获取和失去焦点输入框提示效果的实例代码

这篇文章介绍了jQuery表单获取和失去焦点输入框提示效果的实例代码,有需要的朋友可以参考一下   复制代码 代码如下:   $("#focus .input_txt").each(function(){   var thisVal=$(this).val();   //判断文本框的值是否为空,有值的情况就隐藏提示语,没有值就显示   if(thisVal!=""){   $(this).siblings("span").hide();   }e

Ajax实现动态加载组合框的实例代码

一  province.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <script type="text/javascript" language="javaScript"> var xmlHttp = false; //全局变量,