iOS_UIAlertController的使用方法

原文地址:https://blog.youkuaiyun.com/w582324909/article/details/79012825

iOS 8新增加了UIAlertController控制器,用之前的UIAlertview和actionSheet会报警告,这个控制器可以实现警告框和操作表,非常的方便。使用UIAlertController的优势在于不仅可以添加按钮,还可以添加文本框和自定义视图到警告框和操作表中;相应时间可以通过闭包实现,而不用委托协议实现。下面我就介绍UIAlertController的基本使用方法。

[objc]  view plain  copy
  1. -(void)creatAlertController_sheet {  
  2.       
  3.     /* 
  4.      先创建UIAlertController,preferredStyle:选择UIAlertControllerStyleActionSheet,这个就是相当于创建8.0版本之前的UIActionSheet; 
  5.       
  6.      typedef NS_ENUM(NSInteger, UIAlertControllerStyle) { 
  7.      UIAlertControllerStyleActionSheet = 0, 
  8.      UIAlertControllerStyleAlert 
  9.      } NS_ENUM_AVAILABLE_IOS(8_0); 
  10.       
  11.      */  
  12.     UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"标题" message:@"注释信息,没有就写nil" preferredStyle:UIAlertControllerStyleActionSheet];  
  13.       
  14.     /* 
  15.      typedef NS_ENUM(NSInteger, UIAlertActionStyle) { 
  16.      UIAlertActionStyleDefault = 0, 
  17.      UIAlertActionStyleCancel,         取消按钮 
  18.      UIAlertActionStyleDestructive     破坏性按钮,比如:“删除”,字体颜色是红色的 
  19.      } NS_ENUM_AVAILABLE_IOS(8_0); 
  20.       
  21.      */  
  22.     // 创建action,这里action1只是方便编写,以后再编程的过程中还是以命名规范为主  
  23.     UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"标题1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {  
  24.         NSLog(@"点击了按钮1,进入按钮1的事件");  
  25.     }];  
  26.     UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {  
  27.         NSLog(@"点击了取消");  
  28.     }];  
  29.     UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {  
  30.           
  31.         //跳到创建alertview的方法,一般在点击删除这里按钮之后,都需要一个提示框,提醒用户是否真的删除  
  32.         [self creatAlertController_alert];  
  33.     }];  
  34.       
  35.     //把action添加到actionSheet里  
  36.     [actionSheet addAction:action1];  
  37.     [actionSheet addAction:action2];  
  38.     [actionSheet addAction:action3];  
  39.       
  40.     //相当于之前的[actionSheet show];  
  41.     [self presentViewController:actionSheet animated:YES completion:nil];  
  42. }  
  43.   
  44. //创建一个alertview  
  45. -(void)creatAlertController_alert {  
  46.     //跟上面的流程差不多,记得要把preferredStyle换成UIAlertControllerStyleAlert  
  47.     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"注释信息,没有就写nil" preferredStyle:UIAlertControllerStyleAlert];  
  48.       
  49.     //可以给alertview中添加一个输入框  
  50.     [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {  
  51.         textField.placeholder = @"alert中的文本";  
  52.     }];  
  53.       
  54.     UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"标题1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {  
  55.         NSLog(@"点击了按钮1,进入按钮1的事件");  
  56.         //textFields是一个数组,获取所输入的字符串  
  57.         NSLog(@"%@",alert.textFields.lastObject.text);  
  58.     }];  
  59.     UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {  
  60.         NSLog(@"点击了取消");  
  61.     }];  
  62.       
  63.     [alert addAction:action1];  
  64.     [alert addAction:action2];  
  65.       
  66.     [self presentViewController:alert animated:YES completion:nil];  
  67. }  


效果图如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值