UIAlertViewController類在iOS開發中經常使用,但是使用系統方法需要太多的代碼,所以我自己封裝了一個類。在一個block中實現點擊事件。
- UIAlertController+Category.h
#import <UIKit/UIKit.h>
typedef void (^CallBackBlock)(NSInteger btnIndex);
@interface UIAlertController (Category)
/**
自定義封裝的UIAlertController方法
@param viewController 顯示的vc
@param alertControllerStyle UIAlertControllerStyle 樣式
@param title 標題
@param message 提示信息
@param block 回調block
@param cancelBtnTitle 取消button標題
@param destructiveBtnTitle 紅色的按鈕
@param otherBtnTitles 其他button標題
*/
+ (void)showAlertCntrollerWithViewController:(UIViewController*)viewController alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle title:(NSString*)title message:(NSString*)message CallBackBlock:(CallBackBlock)block cancelButtonTitle:(NSString *)cancelBtnTitle
destructiveButtonTitle:(NSString *)destructiveBtnTitle
otherButtonTitles:(NSString *)otherBtnTitles,...NS_REQUIRES_NIL_TERMINATION;
@end
- UIAlertController+Category.h
#import "UIAlertController+Category.h"
@implementation UIAlertController (Category)
+(void)showAlertCntrollerWithViewController:(UIViewController *)viewController alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle 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:alertControllerStyle];
//添加按鈕
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 : (1)首先在函數里定義一具VA_LIST型的變量,這個變量是指向參數的指針;
* (2)然后用VA_START宏初始化變量剛定義的VA_LIST變量;
* (3)然后用VA_ARG返回可變的參數,VA_ARG的第二個參數是你要返回的參數的類型(如果函數有多個可變參數的,依次調用VA_ARG獲取各個參數);
* (4)最后用VA_END宏結束可變參數的獲取。
* va_start :獲取可變參數列表的第一個參數的地址;
* va_arg :獲取當前參數,返回指定類型并將指針指向下一參數
* va_end :清空va_list可變參數列表:
*
*
*/
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];
}
@end
其實主要的難點就是循環獲取otherButtons,代碼中有詳細的介紹,不足之處希望大家指正。想要了解更多或者下載demo,請訪問github:https://github.com/Maricle1/ControlsPackage.git