您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 中 LGLAlertView 提示框的封裝實作

編輯:iOS 開發綜合

適用於 iOS 8 以後的系統。做法是把系統的 UIAlertController 再做一層封裝,省去每次撰寫大量樣板代碼的麻煩,封裝之後一行就能彈出提示框。

Demo 專案: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);
/** 文字輸入框的回調 block */
typedef void (^TextFieldCallBackBlock)(NSString *text);

@interface LGLAlertView : NSObject

/**
 * 單個或沒有按鈕,不執行任何操作,僅作提示用
 */
+ (void)showAlertViewWith:(UIViewController *)viewController
                    title:(NSString *)title
                  message:(NSString *)message
              buttonTitle:(NSString *)btnTitle
              buttonStyle:(LGLAlertViewActionStyle)buttonStyle;

/**
 * 兩個或多個按鈕:確定、取消
 */
+ (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;

/**
 * 帶輸入框,確定、取消(此處只實作單一輸入框)
 */
+ (void)showAlertTextFieldViewWith:(UIViewController *)viewController
                             title:(NSString *)title
                           message:(NSString *)message
             TextFeildCallBackBlock:(TextFieldCallBackBlock)block
                  cancelButtonTitle:(NSString *)cancelBtnTitle
                  otherButtonTitles:(NSString *)otherBtnTitle;

/**
 * 單個或沒有按鈕的 ActionSheet,僅作提示,按鈕不響應事件
 */
+ (void)showAlertActionSheetViewWith:(UIViewController *)viewController
                               title:(NSString *)title
                             message:(NSString *)message
                         buttonTitle:(NSString *)btnTitle
                         buttonStyle:(LGLAlertViewActionStyle)buttonStyle;

/**
 * 沒有按鈕的 ActionSheet,按鈕有響應事件
 */
+ (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 開始 ==========

// 單個或沒有按鈕
+ (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 = [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 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 performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:LGLAlertShowTime];
    }
}

// ========== AlertView 結束 ==========

#pragma mark ==== 點擊事件 ====
+ (void)dismissAlertController:(UIAlertController *)alert {
    [alert dismissViewControllerAnimated:YES completion:nil];
}

// ========== ActionSheet 開始 ==========

+ (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 = [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 performSelector:@selector(dismissAlertController:) withObject:actionController afterDelay:LGLAlertShowTime];
    }
}

@end

封裝的關鍵在於用可變參數承接任意數量的按鈕,並以 index 回調讓呼叫方自行判斷點了哪一顆。若需要更靈活的樣式,也可以在此基礎上擴充。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved