欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS中 LGLAlertView 提示框的实例代码

程序员文章站 2023-12-20 09:18:58
使用与ios8 以后,只是把系统的uialertcontroller进行了封装,省的每次用的时候要写很多的代码。封装后只需要一句代码即可 , deome 地址 :ht...

使用与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 提示框的实例代码,希望对大家有所帮助

上一篇:

下一篇: