UIAlertViewController類(lèi)在iOS開(kāi)發(fā)中經(jīng)常使用女责,但是使用系統(tǒng)方法需要太多的代碼禾酱,所以我自己封裝了一個(gè)類(lèi)卤唉。在一個(gè)block中實(shí)現(xiàn)點(diǎn)擊事件腹缩。
- UIAlertController+Category.h
#import <UIKit/UIKit.h>
typedef void (^CallBackBlock)(NSInteger btnIndex);
@interface UIAlertController (Category)
/**
自定義封裝的UIAlertController方法
@param viewController 顯示的vc
@param alertControllerStyle UIAlertControllerStyle 樣式
@param title 標(biāo)題
@param message 提示信息
@param block 回調(diào)block
@param cancelBtnTitle 取消button標(biāo)題
@param destructiveBtnTitle 紅色的按鈕
@param otherBtnTitles 其他button標(biāo)題
*/
+ (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)首先在函數(shù)里定義一具VA_LIST型的變量竹祷,這個(gè)變量是指向參數(shù)的指針彰导;
* (2)然后用VA_START宏初始化變量剛定義的VA_LIST變量迟杂;
* (3)然后用VA_ARG返回可變的參數(shù)狭园,VA_ARG的第二個(gè)參數(shù)是你要返回的參數(shù)的類(lèi)型(如果函數(shù)有多個(gè)可變參數(shù)的蓬蝶,依次調(diào)用VA_ARG獲取各個(gè)參數(shù));
* (4)最后用VA_END宏結(jié)束可變參數(shù)的獲取猜惋。
* va_start :獲取可變參數(shù)列表的第一個(gè)參數(shù)的地址;
* va_arg :獲取當(dāng)前參數(shù)丸氛,返回指定類(lèi)型并將指針指向下一參數(shù)
* va_end :清空va_list可變參數(shù)列表:
*
*
*/
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
其實(shí)主要的難點(diǎn)就是循環(huán)獲取otherButtons,代碼中有詳細(xì)的介紹,不足之處希望大家指正著摔。想要了解更多或者下載demo,請(qǐng)?jiān)L問(wèn)github:https://github.com/Maricle1/ControlsPackage.git