你的位置:首页 > 操作系统

[操作系统]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

 




泰国旅游护照签证泰国旅游路线报价费用泰国旅游哪里好玩呢泰国旅游签证怎么办理泰国旅游去哪好一秒穿越到夏天 澳大利亚悉尼旅行生活馆温暖开馆 春运48元飞机票0元火车票开抢 新年寻求新刺激 纵情体验津城“冰火两重天” 外星人来过地球的证据!史上神秘莫测的遗迹(全文) 南山青青世界在什么位置?深圳南山青青世界自驾游线路? 松山湖百花洲一日游价格?东莞松山湖百花洲优惠票怎么买? 松山湖百花洲好不好玩?东莞松山湖百花洲一日游攻略? 樟木头观音山官网?东莞樟木头观音山门票预订? 盘龙峡收费价格表?德庆盘龙峡水世界多少钱? 盘龙峡森林水世界怎么样?德庆盘龙峡森林水世界旅游攻略? 盘龙峡水世界门票多少钱?德庆盘龙峡水世界票价? 盘龙峡水世界有什么玩的?德庆盘龙峡水世界简介? 探访英国哈利波特拍摄地 旅行达人必备六件宝 四川温泉旅游线路推荐_四川泡温泉哪里好_四川哪里有温泉 活死人墓_终南山真实的活死人墓和重阳宫_神雕侠侣活死人墓 MAB10-1104QBQGC Datasheet MAB10-1104QBQGC Datasheet MAB10-1104QBQNC Datasheet MAB10-1104QBQNC Datasheet MAB10-1104QBQYC Datasheet MAB10-1104QBQYC Datasheet 卧龙自然保护区位于 卧龙自然保护区位于 卧龙自然保护区位于 华山在哪 华山在哪 华山在哪 九寨沟在哪个省 九寨沟在哪个省 九寨沟在哪个省