iOS中 LGLAlertView 提示框的執行個體代碼_IOS

來源:互聯網
上載者:User

使用與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 *)titlemessage:(NSString *)message buttonTitle:(NSString *)btnTitlebuttonStyle:(LGLAlertViewActionStyle)buttonStyle;/*** 有兩個或者多個按鈕 確定 取消* @param title 提示的標題* @param message 提示資訊* @param btnTitle 單個按鈕的標題名稱* @param cancelButtonTitle 取消按鈕* @param destructiveBtn destructiveBtn按鈕* @param otherButtonTitles 確定按鈕*/+ (void)showAlertViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)messageCallBackBlock:(CallBackBlock)textBlock cancelButtonTitle:(NSString *)cancelBtnTitledestructiveButtonTitle:(NSString *)destructiveBtnTitleotherButtonTitles:(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 *)messageTextFeildCallBackBlock:(TextFieldCallBackBlock)block cancelButtonTitle:(NSString *)cancelBtnTitleotherButtonTitles:(NSString *)otherBtnTitle;/*** 單個或者沒有按鈕ActionSheet 僅僅只是提示作用 按鈕沒有響應事件* @param title 提示的標題* @param message 提示資訊* @param btnTitle 單個按鈕的標題名稱**/+ (void)showAlertActionSheetViewWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)messagebuttonTitle:(NSString *)btnTitle buttonStyle:(LGLAlertViewActionStyle)buttonStyle;/*** 沒有按鈕ActionSheet 按鈕有響應事件* @param title 提示的標題* @param message 提示資訊* @param btnTitle 單個按鈕的標題名稱**/+ (void)showAlertActionSheetWith:(UIViewController *)viewController title:(NSString *)title message:(NSString *)messagecallbackBlock:(CallBackBlock)block destructiveButtonTitle:(NSString *)destructiveBtnTitlecancelButtonTitle:(NSString *)cancelBtnTitleotherButtonTitles:(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 提示框的執行個體代碼,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.