UIAlertController - 學(xué)習(xí)筆記

一個(gè)網(wǎng)管的iOS學(xué)習(xí)筆記,記錄下自己這條路上的點(diǎn)點(diǎn)滴滴。都是一些很簡(jiǎn)單的筆記,不敢妄談教學(xué),純粹只是為了記錄自己在這條路上——前進(jìn)著。

UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(雖然現(xiàn)在仍可以使用,但是會(huì)有警告)。


UIAlertController 同時(shí)替代了 UIAlertView 和 UIActionSheet,從系統(tǒng)層級(jí)上統(tǒng)一了 alert 的概念 —— 即以 modal 方式或 popover 方式展示。
UIAlertController 是 UIViewController 的子類(lèi),而非其先前的方式。因此新的 alert 可以由 view controller 展示相關(guān)的配置中獲益很多。
UIAlertController 不管是要用 alert 還是 action sheet 方式展示,都要以 title 和 message 參數(shù)來(lái)初始化。Alert 會(huì)在當(dāng)前顯示的 view controller 中心以模態(tài)形式出現(xiàn),action sheet 則會(huì)在底部滑出。Alert 可以同時(shí)有按鈕和輸入框,action sheet 僅支持按鈕。
新的方式并沒(méi)有把所有的 alert 按鈕配置都放在初始化函數(shù)中,而是引入了一個(gè)新類(lèi) UIAlertAction 的對(duì)象,在初始化之后可以進(jìn)行配置。這種形式的 API 重構(gòu)讓對(duì)按鈕數(shù)量、類(lèi)型、順序方便有了更大的控制。同時(shí)也棄用了 UIAlertView 和 UIActionSheet 使用的delegate 這種方式,而是采用更簡(jiǎn)便的完成時(shí)回調(diào)。


正好上面這篇教程是swift的,所以我就寫(xiě)一下Objective-C的吧。

一)新舊對(duì)比:

標(biāo)準(zhǔn)的Alert樣式:
標(biāo)準(zhǔn)的Alert樣式
  • 舊方法:UIAlertView:
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"沒(méi)有標(biāo)題的標(biāo)題"
                                                       message:@"學(xué)無(wú)止境,漫漫長(zhǎng)路"
                                                      delegate:self
                                             cancelButtonTitle:@"取消"
                                             otherButtonTitles:@"確定", nil];
    //顯示alertView
    [alertView show];
  • 新方法:UIAlertController:
    //UIAlertController風(fēng)格:UIAlertControllerStyleAlert
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"沒(méi)有標(biāo)題的標(biāo)題"
                                                                             message:@"學(xué)無(wú)止境,漫漫長(zhǎng)路"
                                                                      preferredStyle:UIAlertControllerStyleAlert ];
    
    //添加取消到UIAlertController中
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    
    //添加確定到UIAlertController中
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:OKAction];
    
    [self presentViewController:alertController animated:YES completion:nil];

標(biāo)準(zhǔn)的Alert Sheet樣式:
標(biāo)準(zhǔn)的Alert Sheet樣式
  • 舊方法:UIActionSheet
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"標(biāo)準(zhǔn)的Action Sheet樣式"
                                                            delegate:self
                                                   cancelButtonTitle:@"取消"
                                              destructiveButtonTitle:@"了解更多"
                                                   otherButtonTitles:@"原來(lái)如此", nil];
    [actionSheet showInView:self.view];
  • 新方法:UIAlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標(biāo)準(zhǔn)的Action Sheet樣式"
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    //取消:style:UIAlertActionStyleCancel
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    //了解更多:style:UIAlertActionStyleDestructive
    UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"了解更多" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:moreAction];
    //原來(lái)如此:style:UIAlertActionStyleDefault
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"原來(lái)如此" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:OKAction];

    [self presentViewController:alertController animated:YES completion:nil];

二)新功能:

UIAlertController 并不只是對(duì)已有的 API 做了清理,而是進(jìn)行了標(biāo)準(zhǔn)化歸納。以前,預(yù)設(shè)的樣式閑置有很多(swizzling 雖然可以提供更多的功能但還是有很大風(fēng)險(xiǎn))。UIAlertController 讓以前看起來(lái)很神奇的事情變?yōu)榱丝赡堋?/code>
ps:摘自——UIAlertController - NSHipster

帶有警示按鈕的 Alert:
帶有警示按鈕的 Alert
  • 這種行為已經(jīng)被UIAlertActionStyle所覆蓋,共有三種類(lèi)型:
style:UIAlertActionStyleDefault//對(duì)按鈕應(yīng)用標(biāo)準(zhǔn)樣式
style:UIAlertActionStyleCancel//對(duì)按鈕應(yīng)用取消樣式,即取消操作
style:UIAlertActionStyleDestructive//對(duì)按鈕應(yīng)用警示性樣式,提示用戶這樣做可能會(huì)刪除或者改變某些數(shù)據(jù)
  • 所以想要對(duì)模態(tài)的 alert 加一個(gè)警示性的按鈕,只需要加上 .Destructive 風(fēng)格的 UIAlertAction 屬性:
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"這是標(biāo)題啊標(biāo)題-標(biāo)題"
                                                                             message:@"這是消息啊消息-消息"
                                                                      preferredStyle:UIAlertControllerStyleAlert ];
    //取消style:UIAlertActionStyleDefault
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    
    //簡(jiǎn)直廢話:style:UIAlertActionStyleDestructive
    UIAlertAction *rubbishAction = [UIAlertAction actionWithTitle:@"簡(jiǎn)直廢話" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:rubbishAction];

    [self presentViewController:alertController animated:YES completion:nil];
有很多個(gè)按鈕的Alert:
有很多個(gè)按鈕的Alert
  • 有 1 個(gè)或者 2 個(gè)操作的時(shí)候,按鈕會(huì)水平排布。更多按鈕的情況,就會(huì)像 action sheet 那樣展示:
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"今天去誰(shuí)家蹭飯呢"
                                                                             message:@"??????\n話說(shuō)可以去好多好多妹紙家里蹭飯"
                                                                      preferredStyle:UIAlertControllerStyleAlert ];
    
    UIAlertAction *home1Action = [UIAlertAction actionWithTitle:@"去小紅家" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:home1Action];
    UIAlertAction *home2Action = [UIAlertAction actionWithTitle:@"去小蘭家" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:home2Action];
    UIAlertAction *home3Action = [UIAlertAction actionWithTitle:@"去小花家" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:home3Action];
    UIAlertAction *home4Action = [UIAlertAction actionWithTitle:@"去小嬌家" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:home4Action];
    
    //取消style:UIAlertActionStyleDefault
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消(回家)" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];

還有“創(chuàng)建登錄表單”和“創(chuàng)建注冊(cè)表單”兩塊內(nèi)容,基本上理解怎么實(shí)現(xiàn),就放明天繼續(xù)完善吧。睡覺(jué)!

最后編輯于
?著作權(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)容