最近剛?cè)胧謱W(xué)習(xí)IOS轩拨,第一個(gè)教程用的是《讓不懂編程的人愛上iPhone開發(fā)(2013 iOS7版)践瓷,其中當(dāng)選擇在IOS 9.x deploy的時(shí)候,教程中UIAlertView已經(jīng)不再適用亡蓉,提示要用UIAlertController替代晕翠,所以學(xué)習(xí)了一下,特在此記錄砍濒。
這次學(xué)習(xí)主要參考了:
Ammar 的 簡書文章
一篇博客:博客
#import"ViewController.h"
@interfaceViewController()
- (IBAction)showAlert:(id)sender;
@end
- (IBAction)showAlert:(id)sender {
//下面內(nèi)容的代碼是放置在這里的
}
UIAlertControllerStyle
有兩種:Alert(對話框), Sheet(上拉菜單)
UIAlertControllerStyleAlert
和 UIAlertControllerStyleActionSheet
UIAlertActionStyle
有三種:Default(常規(guī)), Cancel(取消), Destructive(警示)
UIAlertActionStyleDefault
, UIAlertActionStyleCancel
, UIAlertActionStyleDestructive
現(xiàn)在看下面枚舉的組合例子:
1 UIAlertControllerStyleAlert
(對話框) 添加常規(guī)(okAction) 和 取消(cancelAction) 按鈕:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Hi, Warrior!" message:@"Welcome To The Drag World" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction: okAction];
[alert addAction: cancelAction];
//呈現(xiàn)視圖
[self presentViewController:alert animated:YES completion:nil];
2 UIAlertControllerStyleAlert
(對話框) 添加常規(guī)(okAction) 和警示(destructiveAction) 按鈕:
只需要在上面代碼中把UIAlertActionStyleCancel
替換成 UIAlertActionStyleDestructive
.
3 UIAlertControllerStyleActionSheet
(上拉菜單)添加常規(guī)崖面,取消和警示按鈕梯影, 其中UIAlertActionStyleCancel
的UIAlertAction對象只能有一個(gè)巫员,從顯示中可以明顯看出來:
UIAlertController* alert = [UIAlertControlleralertControllerWithTitle:@"Hi, Warrior!"message:@"Welcome To The Drag World"preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* okAction = [UIAlertActionactionWithTitle:@"OK"style:UIAlertActionStyleDefaulthandler:nil];
UIAlertAction* cancelAction = [UIAlertActionactionWithTitle:@"Cancel"style:UIAlertActionStyleCancelhandler:nil];
UIAlertAction* destructiveAction = [UIAlertActionactionWithTitle:@"Cancel"style:UIAlertActionStyleDestructivehandler:nil];
[alertaddAction: cancelAction];
[alertaddAction: okAction];
[alertaddAction: destructiveAction];
[self presentViewController:alert animated:YES completion:nil];
4 帶文本輸入框的對話框: 使用addTextFieldWithConfigurationHandler
例如:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Hi, Warrior!" message:@"Welcome To The Drag World" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
//配置輸入框內(nèi)的占位符
textField.placeholder=@"Username";
//設(shè)置輸入框的背景顏色简识,其中這里RGB參數(shù)的設(shè)置范圍不是0-255,而是0.0-1.0, alpha 是指背景透明度感猛,0.0為全透明七扰,1.0不透明
textField.backgroundColor = [UIColor colorWithRed:111.0/255 green:206.0/255 blue:250.0/255 alpha:0.5];
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textFiled){
textFiled.placeholder=@"Password";
//設(shè)置密碼的使用安全文本輸入
textFiled.secureTextEntry=YES;}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction: okAction];
[alert addAction: cancelAction];
[alert addAction: deleteAction];
[self presentViewController:alert animated:YES completion:nil ];