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.csdn.net/siwen1990" preferredStyle:UIAlertControllerStyleAlert];  
  23.       
  24.     //UIAlertControllerStyleActionSheet  
  25. //    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"司小文的博客" message:@"http://blog.csdn.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:(有输入框)



UIAlertControllerStyleActionSheet:(无输入框)



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

UIAlertActionStyleDefault 普通

UIAlertActionStyleDestructive 红色

UIAlertActionStyleCancel 取消


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


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值