ios9 提示框的正确使用

 在从iOS8到iOS9的升级过程中,弹出提示框的方式有了很大的改变,在Xcode7 ,iOS9.0的SDK中,已经明确提示不再推荐使用UIAlertView,而只能使用UIAlertController,我们通过代码来演示一下。

我通过点击一个按钮,然后弹出提示框,代码示例如下:

[objc]  view plain copy print ?
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @property(strong,nonatomicUIButton *button;  
  6.   
  7. @end  
  8.   
  9. @implementation ViewController  
  10.   
  11. - (void)viewDidLoad {  
  12.   [super viewDidLoad];  
  13.     
  14.   self.button = [[UIButton alloc] initWithFrame:CGRectMake(0100, [[UIScreen mainScreen] bounds].size.width20)];  
  15.   [self.button setTitle:@"跳转" forState:UIControlStateNormal];  
  16.   [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  17.   [self.view addSubview:self.button];  
  18.     
  19.   [self.button addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside];  
  20.     
  21. }  
  22.   
  23. -(void)clickMe:(id)sender{  
  24.     
  25.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"按钮被点击了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  26.     [alert show];  
  27.     
  28. }  
  29.   
  30. @end  

编写上述代码时,会有下列的警告提示:

“‘UIAlertView’ is deprecated:first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead”.

说明UIAlertView首先在iOS9中被弃用(不推荐)使用。让我们去用UIAlertController。但是运行程序,发现代码还是可以成功运行,不会出现crash。

     但是在实际的工程开发中,我们有这样一个“潜规则”:要把每一个警告(warning)当做错误(error)。所以为了顺应苹果的潮流,我们来解决这个warning,使用UIAlertController来解决这个问题。代码如下:

[objc]  view plain copy print ?
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @property(strong,nonatomicUIButton *button;  
  6.   
  7. @end  
  8.   
  9. @implementation ViewController  
  10.   
  11. - (void)viewDidLoad {  
  12.   [super viewDidLoad];  
  13.     
  14.   self.button = [[UIButton alloc] initWithFrame:CGRectMake(0100, [[UIScreen mainScreen] bounds].size.width20)];  
  15.   [self.button setTitle:@"跳转" forState:UIControlStateNormal];  
  16.   [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  17.   [self.view addSubview:self.button];  
  18.     
  19.   [self.button addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside];  
  20.     
  21. }  
  22.   
  23. -(void)clickMe:(id)sender{  
  24.     
  25.   //初始化提示框;  
  26.   UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"按钮被点击了" preferredStyle UIAlertControllerStyleAlert];  
  27.     
  28.   [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {  
  29.     //点击按钮的响应事件;  
  30.   }]];  
  31.     
  32.   //弹出提示框;  
  33.   [self presentViewController:alert animated:true completion:nil];  
  34.     
  35.     
  36. }  
  37.   
  38.   
  39.   
  40. @end  

这样,代码就不会有警告了。

程序运行后的效果同上。  其中preferredStyle这个参数还有另一个选择:UIAlertControllerStyleActionSheet。选择这个枚举类型后,实现效果如下:

发现这个提示框是从底部弹出的。是不是很简单呢?通过查看代码还可以发现,在提示框中的按钮响应不再需要delegate委托来实现了。直接使用addAction就可以在一个block中实现按钮点击,非常方便。



github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值