UIAlertController(iOS8)是 UIAlertView和UIActionSheet?替代和升級版。
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í),就會調(diào)用蜜唾,并且當(dāng)方法調(diào)完后仅醇,UIAlertView會自動(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