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 提示框的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • iOS应用开发中UIView添加边框颜色及设置圆角边框的方法

    UIView加边框及边框颜色 引用库: 复制代码 代码如下: #import <QuartzCore/QuartzCore.h> 使用: 复制代码 代码如下: //添加边框和提示         CGRect frameRect = CGRectMake(20, 90, self.window.frame.size.width-40, self.window.frame.size.height-180);         UIView   *frameView = [[UIView alloc

  • iOS毛玻璃效果的实现及图片模糊效果的三种方法

    App设计时往往会用到一些模糊效果或者毛玻璃效果,iOS目前已提供一些模糊API可以让我们方便是使用. 话说苹果在iOS7.0之后,很多系统界面都使用了毛玻璃效果,增加了界面的美观性,比如下图的通知中心界面; 但是其iOS7.0的SDK并没有提供给开发者实现毛玻璃效果的API,所以很多人都是通过一些别人封装的框架来实现,后面我也会讲到一个; 其实在iOS7.0(包括)之前还是有系统的类可以实现毛玻璃效果的, 就是 UIToolbar这个类,并且使用相当简单,几行代码就可以搞定. 下面是代码实现:

  • 基于IOS实现带箭头的view

    我使用DrawRect进行的View的拉伸(是这样描述的吧??), 效果图也实现了类似于微信的View效果, 你可以看一看. 创建继承于UIView的视图 .h文件 // backGoundView @property (nonatomic, strong) UIView * _Nonnull backGoundView; // titles @property (nonatomic, strong) NSArray * _Nonnull dataArray; // images @proper

  • iOS设置可选择圆角方向的控件圆角

    前言 这篇文章主要给大家介绍利用iOS如何设置可选择圆角方向的控件圆角,话不多说,以下是实现的示例代码,一起来看看吧. 示例代码 一.通过设置控件layer的cornerRadius来设置圆角 self.view.layer.cornerRadius =10.f;//如果设置圆角角度为半圆,则数值为控件高度的一半 self.view.layer.masksToBounds = YES;//是否删除多余的位置 二.通过贝塞尔曲线来设置圆角 UIBezierPath *maskPath = [UIB

  • IOS设置按钮为圆角的示例代码

    iOS中很多时候都需要用到指定风格的圆角按钮,以下是UIButton提供的创建圆角按钮方法 设置按钮的4个角: 左上:UIRectCornerTopLeft 左下:UIRectCornerBottomLeft 右上:UIRectCornerTopRight 右下:UIRectCornerBottomRight 示例代码: UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 60, 80, 40)]; button.b

  • iOS自定义推送消息提示框

    看到标题你可能会觉得奇怪 推送消息提示框不是系统自己弹出来的吗? 为什么还要自己自定义呢? 因为项目需求是这样的:最近需要做 远程推送通知 和一个客服系统 包括店铺客服和官方客服两个模块 如果有新的消息推送的时候 如果用户当前不在客服界面的时候  要求无论是在app前台 还是app退到后台 顶部都要弹出系统的那种消息提示框 这样的需求 我们就只能自定义一个在app内 弹出消息提示框 实现步骤如下: 1.我们自定义一个view 为 STPushView 推送消息的提示框view  #import

  • IOS开发代码分享之设置UISearchBar的背景颜色

    今天用到UISearchBar,之前网上提供的方法已经不能有效的去除掉它的背景色了,修改背景色方法如下: mySearchBar.backgroundColor = RGBACOLOR(249,249,249,1);     mySearchBar.backgroundImage = [self imageWithColor:[UIColor clearColor] size:mySearchBar.bounds.size];   //取消searchbar背景色 - (UIImage *)im

  • iOS实现圆角箭头矩形的提示框

    先来看看我们见过的一些圆角箭头矩形的提示框效果图 一.了解CGContextRef 首先需要对 CGContextRef 了解, 作者有机会再进行下详细讲解, 这篇中简单介绍下, 方便后文阅读理解. 先了解 CGContextRef 坐标系 坐标系 举例说明 : 对于 商城类App 有很多原价, 现价对比 .那 原件的横线怎么画, 就可以用CGContextRef - (void)drawRect:(CGRect)rect { // Drawing code [super drawRect:re

  • iOS9提示框的正确使用方式

    在从iOS8到iOS9的升级过程中,弹出提示框的方式有了很大的改变,在Xcode7 ,iOS9.0的SDK中,苹果已经明确提示不再推荐使用UIAlertView,而推荐使用UIAlertController,现在,我们通过代码来演示一下. #import "LoginViewController.h" @interface LoginViewController () @property (weak, nonatomic) IBOutlet UITextField *passWord;

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

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

随机推荐