iOS簡單調(diào)用系統(tǒng)提示框(UIAlertController)

iOS9開始UIAlertView不被推薦使用滓玖,取而代之的是iOS8推出的UIAlertController详拙。
從它們的命名就能看得出一個是繼承了UIView,一個是繼承了UIViewController遍尺。不少小伙伴看到這個變動后當(dāng)場就不開心了截酷。以前的UIAlertView到處可以調(diào)用,并且宏定義起來調(diào)用也非常方便乾戏。

#define Alert(msg) \
[[[UIAlertView alloc] initWithTitle:@"" \
message:msg \
delegate:nil \
cancelButtonTitle:@"確定" \
otherButtonTitles:nil, nil] show]; \

可是現(xiàn)在的UIAlertController是需要通過presentViewController來彈窗的,所以必須要拿到Controller才能調(diào)用合搅,這樣很不方便多搀。讓我們來看看有沒有好的方法來改造一下呢。

要不來點(diǎn)簡單粗暴的吧~


首先最最關(guān)鍵的就是獲取當(dāng)前顯示的ViewController灾部,也是頂層的ViewController康铭。

#pragma mark - Utils
- (UIViewController *)getCurrentViewController
{
    UIViewController *currentViewController = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if ([window subviews].count == 0) {
        return nil;
    }
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * subWindow in windows)
        {
            if (subWindow.windowLevel == UIWindowLevelNormal) {
                window = subWindow;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        currentViewController = nextResponder;
    } else {
        currentViewController = window.rootViewController;
    }
    return currentViewController;
}

獲取到當(dāng)前的ViewController后,就可以配置UIAlertController進(jìn)行跳轉(zhuǎn)赌髓,方法命名模仿UIAlertView,并且用Block代替了UIAlertDelegate中的alertView:clickedButtonAtIndex:代理方法从藤。

#pragma mark -- base method
/**
 *  提示框基礎(chǔ)方法
 *
 *  @param title             標(biāo)題
 *  @param message           消息
 *  @param cancelButtonTitle 取消按鈕標(biāo)題
 *  @param cancelCallBack    取消按鈕回調(diào)
 *  @param otherCallBack     其他按鈕回調(diào)
 *  @param otherButtonTitles 其他按鈕
 */
- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle cancelCallBack:(CancelCallBack)cancelCallBack otherCallBack:(OtherCallBack)otherCallBack otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION{
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
    //初始化AlertController
    UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    //定義va_list
    va_list argsList;
    //指向首地址
    va_start(argsList, otherButtonTitles);
    NSInteger index = 0;
    //遍歷
    while (otherButtonTitles) {
        //過濾入?yún)㈩愋?        if (![otherButtonTitles isKindOfClass:[NSString class]]) {
            break;
        }
        //過濾空字符串
        if (![self isBlankString:otherButtonTitles]) {
            //添加提示框按鈕動作
            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:otherButtonTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (otherCallBack) {
                    [alertViewController dismissViewControllerAnimated:YES completion:nil];
                    otherCallBack(index);
                }
            }];
            [alertViewController addAction:alertAction];
        }
        //指向下一個地址
        otherButtonTitles = va_arg(argsList, NSString *);
        index++;
    }
    va_end(argsList);
    
    //取消按鈕
    if (![self isBlankString:cancelButtonTitle]) {
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //點(diǎn)擊過后關(guān)閉提示框
            if (cancelCallBack) {
                [alertViewController dismissViewControllerAnimated:YES completion:nil];
                //取消回調(diào)方法
                cancelCallBack();
            }
        }];
        [alertViewController addAction:alertAction];
    }
    //從當(dāng)前控制器中模態(tài)彈出提示框
    [[self getCurrentViewController] presentViewController:alertViewController animated:YES completion:nil];
    
#else

    self.cancelCallBack = [cancelCallBack copy];
    self.otherCallBack = [otherCallBack copy];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
    va_list argsList;
    va_start(argsList, otherButtonTitles);
    NSInteger index = 0;
    while (otherButtonTitles) {
        if (![self isBlankString:otherButtonTitles]) {
            [alertView addButtonWithTitle:otherButtonTitles];
        }
        otherButtonTitles = va_arg(argsList, NSString *);
        index++;
    }
    va_end(argsList);
    
    [alertView show];
}

常用的類方法以及對象方法也進(jìn)行了淺層封裝

+ (instancetype)alertView {
    return [[[self class] alloc] init];
}

#pragma mark - public method
#pragma mark -- class method
/**
 *  類方法  提示框(只顯示消息)
 *
 *  @param message 消息
 */
+ (void)showMessage:(NSString *)message {
    [[[self class] alertView] showMessage:message];
}

/**
 *  類方法  提示框(只顯示標(biāo)題和消息)
 *
 *  @param title   標(biāo)題
 *  @param message 消息
 */
+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message {
    [[[self class] alertView] showAlertViewWithTitle:title message:message];
}

/**
 *  類方法  提示框(只顯示標(biāo)題和消息)
 *
 *  @param title   標(biāo)題
 *  @param message 消息
 *  @param dismissCallBack 確定回調(diào)
 */
+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message dismissCallBack:(CancelCallBack)dismissCallBack {
    [[[self class] alertView] showAlertViewWithTitle:title message:message dismissCallBack:dismissCallBack];
}

#pragma mark -- object method
/**
 *  提示框(只顯示消息)
 *
 *  @param message 消息
 */
- (void)showMessage:(NSString *)message {
    [self showAlertViewWithTitle:nil message:message];
}

/**
 *  提示框(只顯示標(biāo)題和消息)
 *
 *  @param title   標(biāo)題
 *  @param message 消息
 */
- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message {
    [self showAlertViewWithTitle:title message:message cancelButtonTitle:@"確定" cancelCallBack:nil otherCallBack:nil otherButtonTitles:nil];
}

/**
 *  提示框(只顯示標(biāo)題和消息)
 *
 *  @param title   標(biāo)題
 *  @param message 消息
 *  @param dismissCallBack 確定回調(diào)
 */
- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message dismissCallBack:(CancelCallBack)dismissCallBack {
    [self showAlertViewWithTitle:title message:message cancelButtonTitle:@"確定" cancelCallBack:dismissCallBack otherCallBack:nil otherButtonTitles:nil, nil];
}

至此,我們的目標(biāo)也就達(dá)成了锁蠕。我們可以用宏定義簡單的調(diào)用系統(tǒng)提示框了

#define XXALERT(msg) [ALAlertView showMessage:msg];

注意事項:

  1. 若需要界面顯示時夷野,就出現(xiàn)彈窗,建議在ViewDidAppear中調(diào)用荣倾。
  2. 設(shè)備是iOS9及之后版本的悯搔,同時彈出多個窗口時只會顯示第一個,建議在cancelCallBack中多次彈出舌仍。
  3. 多個按鈕的入?yún)⒘斜?strong>otherButtonTitles的順序與otherCallBack中的返回的下標(biāo)是一一對應(yīng)的妒貌,從0開始。

Demo地址:
https://github.com/alanshen0118/ALAlertView.git

若有問題铸豁,小編會及時更正灌曙,請大家多多指教。
若覺得對您有用节芥,請點(diǎn)下方喜歡在刺,多謝支持~

Thanks~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市头镊,隨后出現(xiàn)的幾起案子蚣驼,更是在濱河造成了極大的恐慌,老刑警劉巖相艇,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件颖杏,死亡現(xiàn)場離奇詭異,居然都是意外死亡厂捞,警方通過查閱死者的電腦和手機(jī)输玷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來靡馁,“玉大人欲鹏,你說我怎么就攤上這事〕裟” “怎么了赔嚎?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我尤误,道長侠畔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任损晤,我火速辦了婚禮软棺,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘尤勋。我一直安慰自己喘落,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布最冰。 她就那樣靜靜地躺著瘦棋,像睡著了一般。 火紅的嫁衣襯著肌膚如雪暖哨。 梳的紋絲不亂的頭發(fā)上赌朋,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機(jī)與錄音篇裁,去河邊找鬼沛慢。 笑死,一個胖子當(dāng)著我的面吹牛茴恰,可吹牛的內(nèi)容都是我干的颠焦。 我是一名探鬼主播斩熊,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼往枣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了粉渠?” 一聲冷哼從身側(cè)響起分冈,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎霸株,沒想到半個月后雕沉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡去件,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年坡椒,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尤溜。...
    茶點(diǎn)故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡倔叼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出宫莱,到底是詐尸還是另有隱情丈攒,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站巡验,受9級特大地震影響际插,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜显设,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一框弛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捕捂,春花似錦功咒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至幽七,卻和暖如春景殷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背澡屡。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工猿挚, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人驶鹉。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓绩蜻,卻偏偏與公主長得像,于是被迫代替她去往敵國和親室埋。 傳聞我的和親對象是個殘疾皇子办绝,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評論 2 354

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件姚淆、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,094評論 4 62
  • “你們都是要刺殺秦王的嗎孕蝉?”小陸驚訝地問道,“看來這秦王樹敵無數(shù)腌逢,想要刺殺他的人居然都結(jié)成了幫會降淮!” “難...
    擁衾聽雨待晚虹閱讀 182評論 0 1
  • 1 林格和楊菲是很要好的朋友,他們初高中一直在一起搏讶,彼此之間有很多共同的朋友佳鳖。大學(xué)畢業(yè)后回到他們老家A市,一起到一...
    風(fēng)兒飛閱讀 406評論 4 3
  • 孩子不僅是父母愛情的結(jié)晶淑玫,更是家庭幸福的紐帶,當(dāng)小生命呱呱墜地,父母家人的心都被其緊抓絮蒿,但至陽之體尊搬,稍不留神就可能...
    閱讀中醫(yī)閱讀 533評論 0 0