iOS開發(fā):UIAlertController

UIAlertController(iOS8)是 UIAlertViewUIActionSheet?替代和升級(jí)版。

UIAlertController 繼承 UIViewController ,所以彈出方式可以用 模態(tài)彈出


1:初始化

初始化代碼:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"我是提示的內(nèi)容" preferredStyle:UIAlertControllerStyleAlert ];

初始化選擇 UIAlertController風(fēng)格:

style:UIAlertControllerStyleAlert? (Alert)

style:UIAlertControllerStyleActionSheet(ActionSheet)

2:添加按鈕

取消按鈕(靠左):

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

[alertController addAction:cancelAction];

確定按鈕(靠右):

UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];

[alertController addAction:OKAction];

按鈕風(fēng)格:

style:UIAlertActionStyleDefault-->標(biāo)準(zhǔn)樣式

style:UIAlertActionStyleCancel-->取消樣式,即取消操作

style:UIAlertActionStyleDestructive-->警示性樣式(字體紅色)

3:顯示

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


舊方法:(UIAlertView)

1:初始化

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"溫馨提 message:@"我是提示信息" delegate:self ?cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];

2:顯示alertView

[alertView show];

3:代理方法:(UIAlertViewDelegate)

1.當(dāng)點(diǎn)擊UIAlertView上的按鈕時(shí),就會(huì)調(diào)用,并且當(dāng)方法調(diào)完后,UIAlertView會(huì)自動(dòng)消失。

- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

2.當(dāng)UIAlertView即將出現(xiàn)的時(shí)候調(diào)用

- (void)willPresentAlertView:(UIAlertView*)alertView;

3.當(dāng)UIAlertView完全出現(xiàn)的時(shí)候調(diào)用

- (void)didPresentAlertView:(UIAlertView*)alertView;

4.當(dāng)UIAlertView即將消失的時(shí)候調(diào)用

- (void)alertView:(UIAlertView*)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;

5.當(dāng)UIAlertView完全消失的時(shí)候調(diào)用

- (void)alertView:(UIAlertView*)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

4:補(bǔ)充

? ? ?如果要修改alertView控件的frame,比如text位置是居中的,如果要讓其向左偏移,在代理方法2中遍歷alertView.subviews,判斷控件所屬的class,修改即可。

更多方法:重溫UIAlertView


舊方法:(UIActionSheet)

1:初始化:

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@" 我是Action Shee樣式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"我是紅色" otherButtonTitles:@"原來如此", nil];

2:顯示actionSheet

[actionSheet showInView:self.view];

3:代理方法(UIActionSheetDelegate)

1.點(diǎn)擊按鈕時(shí)調(diào)用

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

2.視圖將要彈出時(shí)調(diào)用

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;

3.視圖已經(jīng)彈出時(shí)調(diào)用

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet;

4.點(diǎn)擊按鈕后,視圖將要消失時(shí)調(diào)用

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;

5.點(diǎn)擊按鈕后,視圖已經(jīng)消失時(shí)調(diào)用

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;

補(bǔ)充:

關(guān)于UIActionSheet中如果按鈕點(diǎn)擊事件是跳轉(zhuǎn)到其他控制器的時(shí)候,最好在代理5中執(zhí)行。

更多方法:UIActionSheet


學(xué)無止境,做個(gè)記錄

2017-02-05-SXH

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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