在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 ]