UIAlertController簡單使用

在iOS8之前的開發(fā)過程中,我們通常使用UIAlertView或者UIActionSheet來提示用戶是否進(jìn)行某項(xiàng)操作,但是其使用都過于繁瑣

【1】 UIAlertView和UIActionSheet 的使用過程

UIAlertView *alertView =[ [UIAlertView alloc] initWithTitle: message: delegate: cancelButtonTitle: otherButtonTitles: ];
;這樣的形式來聲明一個(gè)AlertView,UIActionSheet 類似
但是我們?nèi)绻o其中的「確認(rèn)」「取消」按鈕添加相應(yīng)的方法,就得添加UIActionSheetDelegate或者UIAlertViewDelegate,然后實(shí)現(xiàn) 對應(yīng)的 ClickAtButtonIndex方法,過程比較繁瑣,所以在IOS8以后 apple推出了 UIAlertController將功能更加集成。

【2】 UIAlertController使用

[1]聲明:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"確認(rèn)?" ] preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertControllerStyle:
    #UIAlertControllerStyleActionSheet = 0,//抽屜
    #UIAlertControllerStyleAlert//警告框

這個(gè)屬性區(qū)分了actionSheet和alertView

[2] 使用

  apple將alertView和actionSheet中button重新聲明了一個(gè)類  UIAlertAction
  ---<1>UIAlertAction初始化
       UIAlertAction *alertAction = [UIAlertAction actionWithTitle:
                                                              style: 
                                                            handler:];
        style:
            UIAlertActionStyleDefault = 0,  //默認(rèn)的風(fēng)格
            UIAlertActionStyleCancel,       //取消按鈕的風(fēng)格
            UIAlertActionStyleDestructive   //警告的風(fēng)格 (通常被用作"確認(rèn)"按鈕)
     handler中就是點(diǎn)擊該Action會執(zhí)行的操作
  ---<2>UIAlertAction 使用
     [ alertController addAction:alertAction ];
     使用更加便捷
  ---<3>UIAlertController使用
 [self presentViewController:alertController animated: YES completion: nil ]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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