iOS UIAlertController代替代替UIAlertView与UIActionSheet

UIAlertController是用来代替之前我们使用的UIAlertView和UIActionSheet,这次的改进总体来讲,感觉思路更清晰简洁了,使用起来也是颇为顺手,下面不多说老样子上代码:

  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.     self.view.backgroundColor = [UIColor whiteColor];  
  12.       
  13.     [self alertViewcontrol];  
  14.     // Do any additional setup after loading the view, typically from a nib.  
  15. }  
  16.   
  17. -(void)alertViewcontrol  
  18.   
  19. {  
  20.       
  21.     //UIAlertControllerStyleAlert  
  22.     UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"司小文的博客" message:@"http://blog.****.net/siwen1990" preferredStyle:UIAlertControllerStyleAlert];  
  23.       
  24.     //UIAlertControllerStyleActionSheet  
  25. //    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"司小文的博客" message:@"http://blog.****.net/siwen1990" preferredStyle:UIAlertControllerStyleActionSheet];  
  26.   
  27.       
  28.     UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {  
  29.         //普通按钮  
  30.         NSLog(@"我是普通按钮");  
  31.     }];  
  32.       
  33.     UIAlertAction *aaaAction = [UIAlertAction actionWithTitle:@"aaa" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {  
  34.         //红色按键  
  35.         NSLog(@"我是红色按键");  
  36.     }];  
  37.   
  38.     UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {  
  39.         //取消按钮  
  40.         NSLog(@"我是取消按钮");  
  41.     }];  
  42.       
  43.       
  44.     //如果是UIAlertControllerStyleActionSheet 不能使用添加输入框的方法  
  45.     [alertControl addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {  
  46.         //添加输入框(已经自动add,不需要手动)  
  47.           
  48.         textField.text = @"可以在这里写textfield的一些属性";  
  49.           
  50.         //监听  
  51.         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listeningTextField:) name:UITextFieldTextDidChangeNotification object:textField];  
  52.           
  53.     }];  
  54.       
  55.     //添加按钮(按钮的排列与添加顺序一样,唯独取消按钮会一直在最下面)  
  56.     [alertControl addAction:okAction];//ok  
  57.     [alertControl addAction:aaaAction];//aaa  
  58.     [alertControl addAction:cancelAction];//cancel  
  59.       
  60.     //显示警报框  
  61.     [self presentViewController:alertControl animated:YES completion:nil];  
  62.       
  63. }  
  64.   
  65. //监听弹框上的输入内容的变化  
  66. -(void)listeningTextField:(NSNotification *)notionfication  
  67. {  
  68.     UITextField *thisTextField = (UITextField*)notionfication.object;  
  69.     NSLog(@"%@",thisTextField.text);  
  70. }  
  71.   
  72. - (void)didReceiveMemoryWarning {  
  73.     [super didReceiveMemoryWarning];  
  74.     // Dispose of any resources that can be recreated.  
  75. }  


UIAlertController中的显示方式有两种只是在创建时不同,其他的方法基本都是一样的

UIAlertControllerStyleAlert:(有输入框)

iOS UIAlertController代替代替UIAlertView与UIActionSheet


UIAlertControllerStyleActionSheet:(无输入框)

iOS UIAlertController代替代替UIAlertView与UIActionSheet

UIAlertController可以添加的按钮总共有三类

UIAlertActionStyleDefault 普通

UIAlertActionStyleDestructive 红色

UIAlertActionStyleCancel 取消


主要记得,在改变UIAlertControllerStyleActionSheet的时候一定不要加上输入框,不然会崩溃呦~


本文为转载 原文地址:http://blog.****.net/siwen1990