iOS 之UIAlertController的使用方法

来源:互联网 发布:命令者模式 java 编辑:程序博客网 时间:2024/06/07 05:16

UIAlertView在IOS 8以上版本已经过时了,官方推荐我们使用UIAlertController代替UIAlertView、UIActionSheet


1、UIAlertController显示普通的Alert

[objc] view plain copy
  1. - (IBAction)showAlert:(UIButton *)sender {  
  2.     //显示提示框  
  3.     //过时  
  4. //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];  
  5. //    [alert show];  
  6.     UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"  
  7.                                                                    message:@"确定删除该设备信息?"  
  8.                                                             preferredStyle:UIAlertControllerStyleAlert];  
  9.       
  10.     UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault  
  11.                                                           handler:^(UIAlertAction * action) {  
  12.                                                               //响应事件  
  13.                                                               NSLog(@"action = %@", action);  
  14.                                                           }];  
  15.     UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault  
  16.                                                           handler:^(UIAlertAction * action) {  
  17.                                                               //响应事件  
  18.                                                               NSLog(@"action = %@", action);  
  19.                                                           }];  
  20.   
  21.     [alert addAction:defaultAction];  
  22.     [alert addAction:cancelAction];  
  23.     [self presentViewController:alert animated:YES completion:nil];  
  24. }  

2、UIAlertController显示带文本输入的的Alert

[objc] view plain copy
  1. - (IBAction)showList:(UIButton *)sender {  
  2.     //提示框添加文本输入框  
  3.     UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"  
  4.                                                                    message:@"This is an alert."  
  5.                                                             preferredStyle:UIAlertControllerStyleAlert];  
  6.       
  7.     UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault  
  8.                                                           handler:^(UIAlertAction * action) {  
  9.                                                               //响应事件  
  10.                                                               //得到文本信息  
  11.                                                               for(UITextField *text in alert.textFields){  
  12.                                                                   NSLog(@"text = %@", text.text);  
  13.                                                               }  
  14.                                                           }];  
  15.     UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel  
  16.                                                          handler:^(UIAlertAction * action) {  
  17.                                                              //响应事件  
  18.                                                              NSLog(@"action = %@", alert.textFields);  
  19.                                                          }];  
  20.     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {  
  21.         textField.placeholder = @"登录";  
  22.     }];  
  23.     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {  
  24.         textField.placeholder = @"密码";  
  25.         textField.secureTextEntry = YES;  
  26.     }];  
  27.       
  28.     [alert addAction:okAction];  
  29.     [alert addAction:cancelAction];  
  30.     [self presentViewController:alert animated:YES completion:nil];  
  31.   
  32. }  

3、UIAlertController显示ActionSheet


- (IBAction)showSheet:(UIButton *)sender {      //显示弹出框列表选择      UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"                                                                     message:@"This is an Sheet."                                                              preferredStyle:UIAlertControllerStyleActionSheet];            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel                                                            handler:^(UIAlertAction * action) {                                                                //响应事件                                                                NSLog(@"action = %@", action);                                                            }];      UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive                                                           handler:^(UIAlertAction * action) {                                                               //响应事件                                                               NSLog(@"action = %@", action);                                                           }];      UIAlertAction* saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault                                                           handler:^(UIAlertAction * action) {                                                               //响应事件                                                               NSLog(@"action = %@", action);                                                           }];      [alert addAction:saveAction];      [alert addAction:cancelAction];      [alert addAction:deleteAction];      [self presentViewController:alert animated:YES completion:nil];  } 




原文章地址:IOS使用UIAlertController三种显示提示框代码

原创粉丝点击