iOS9使用提示框的正确实现方式

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

[[154064]]

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

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

 

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

 


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

 

  1. “‘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来解决这个问题。代码如下:

 

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

 


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

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

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

责任编辑:chenqingxiang 来源: 乞力马扎罗的雪雪
相关推荐

2010-02-24 10:07:48

WCF跨越边界

2010-03-04 15:12:33

Python算法

2015-07-16 12:59:19

IOS9UIDynamics

2010-02-25 10:10:29

WCF使用Header

2010-01-25 15:23:12

Android横竖屏切

2010-02-26 11:22:16

LitwareHR使用

2010-02-24 13:48:44

MSMQ使用WCF

2015-10-16 14:27:29

iOS9collectionV特性

2015-09-16 09:55:12

ios9学习UIKit Dynam

2021-01-28 14:34:35

鸿蒙HarmonyOS应用开发

2015-08-24 09:24:21

ios学习contacts fr

2015-08-20 09:00:23

ios9api

2015-07-02 17:32:28

iOS 9苹果

2015-09-25 09:44:24

ios9MapkitTrans

2010-06-09 09:34:11

2009-12-03 11:11:57

PHP网站优化

2009-12-29 18:09:00

Silverlight

2010-03-04 11:12:02

Python AOP

2010-01-06 15:56:18

.Net Framew

2016-03-18 11:19:57

ios9replaykit入门
点赞
收藏

51CTO技术栈公众号