UIAlertController簡(jiǎn)單封裝和不定參數(shù)

我們?cè)谑褂肬IAlertController時(shí)一般會(huì)這樣

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"這是一個(gè)Alert" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *alertAction0 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
       
    }];
UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
    }];

[alert addAction:alertAction0];
[alert addAction:alertAction1];
//或者用for循環(huán)添加Action

這樣寫(xiě)一次兩次還好,如果要多次用到Alert,每次都要重復(fù)一次這一堆。于是就簡(jiǎn)單地封裝了一下,把這一堆放進(jìn)一個(gè)方法中。
像這樣:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"這是一個(gè)簡(jiǎn)單封裝的Alert" preferredStyle:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
        if ([action.title isEqualToString:@"取消"]) {
            NSLog(@"點(diǎn)擊了>>>>取消");
        }
        if ([action.title isEqualToString:@"確定"]) {
            NSLog(@"點(diǎn)擊了>>>>確定");
        }
        if ([action.title isEqualToString:@"ok"]) {
            NSLog(@"點(diǎn)擊了>>>>ok");
        }
    } cancelButtonTitle:@"取消" otherButtonTitles:@"確定",@"ok", nil];
   // [self presentViewController:alert animated:YES completion:nil];

這樣感覺(jué)清爽不少_
這里用到了不定參數(shù),關(guān)于其使用我在方法的實(shí)現(xiàn)中已經(jīng)注釋?zhuān)信d趣的同學(xué)可以去下載demo查看。

時(shí)間倉(cāng)促,只是簡(jiǎn)單封裝,有誤之處還請(qǐng)指正。

這一篇文章介紹iOS鏈?zhǔn)骄幊?/code>iOS,Objective-C鏈?zhǔn)骄幊毯?jiǎn)談。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容