iOS常見(jiàn)控件的封裝(二):UIAlertViewController

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末缓窜,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子谍咆,更是在濱河造成了極大的恐慌禾锤,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摹察,死亡現(xiàn)場(chǎng)離奇詭異恩掷,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)供嚎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)黄娘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人克滴,你說(shuō)我怎么就攤上這事逼争。” “怎么了劝赔?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵誓焦,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我着帽,道長(zhǎng)杂伟,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任仍翰,我火速辦了婚禮稿壁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘歉备。我一直安慰自己,他們只是感情好匪燕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布蕾羊。 她就那樣靜靜地躺著,像睡著了一般帽驯。 火紅的嫁衣襯著肌膚如雪龟再。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天尼变,我揣著相機(jī)與錄音利凑,去河邊找鬼浆劲。 笑死,一個(gè)胖子當(dāng)著我的面吹牛哀澈,可吹牛的內(nèi)容都是我干的牌借。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼割按,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼膨报!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起适荣,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤现柠,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后弛矛,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體够吩,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年丈氓,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了周循。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡扒寄,死狀恐怖鱼鼓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情该编,我是刑警寧澤迄本,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站课竣,受9級(jí)特大地震影響嘉赎,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜于樟,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一公条、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧迂曲,春花似錦靶橱、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至杰扫,卻和暖如春队寇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背章姓。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工佳遣, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留识埋,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓零渐,卻偏偏與公主長(zhǎng)得像窒舟,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子相恃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容