iOS 之UIAlertController的使用方法

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三種顯示提示框代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章