自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS9使用提示框的正確實(shí)現(xiàn)方式

移動(dòng)開(kāi)發(fā) iOS
在從iOS8到iOS9的升級(jí)過(guò)程中,彈出提示框的方式有了很大的改變,在Xcode7 ,iOS9.0的SDK中,已經(jīng)明確提示不再推薦使用UIAlertView,而只能使用UIAlertController,我們通過(guò)代碼來(lái)演示一下。

[[154064]]

在從iOS8到iOS9的升級(jí)過(guò)程中,彈出提示框的方式有了很大的改變,在Xcode7 ,iOS9.0的SDK中,已經(jīng)明確提示不再推薦使用UIAlertView,而只能使用UIAlertController,我們通過(guò)代碼來(lái)演示一下。

我通過(guò)點(diǎn)擊一個(gè)按鈕,然后彈出提示框,代碼示例如下:

 

  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:@"跳轉(zhuǎn)" 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:@"按鈕被點(diǎn)擊了" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil]; 
  27. [alert show]; 
  28.  
  29.  
  30. @end  

 


編寫(xiě)上述代碼時(shí),會(huì)有下列的警告提示:

 

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

說(shuō)明UIAlertView首先在iOS9中被棄用(不推薦)使用。讓我們?nèi)ビ肬IAlertController。但是運(yùn)行程序,發(fā)現(xiàn)代碼還是可以成功運(yùn)行,不會(huì)出現(xiàn)crash。

但是在實(shí)際的工程開(kāi)發(fā)中,我們有這樣一個(gè)“潛規(guī)則”:要把每一個(gè)警告(warning)當(dāng)做錯(cuò)誤(error)。所以為了順應(yīng)蘋(píng)果的潮流,我們來(lái)解決這個(gè)warning,使用UIAlertController來(lái)解決這個(gè)問(wèn)題。代碼如下:

 

  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:@"跳轉(zhuǎn)" 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:@"按鈕被點(diǎn)擊了" preferredStyle: UIAlertControllerStyleAlert]; 
  28.  
  29. [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
  30. //點(diǎn)擊按鈕的響應(yīng)事件; 
  31. }]]; 
  32.  
  33. //彈出提示框; 
  34. [self presentViewController:alert animated:true completion:nil]; 
  35.  
  36.  
  37.  
  38.  
  39.  
  40. @end  

 


這樣,代碼就不會(huì)有警告了。

程序運(yùn)行后的效果同上。 其中preferredStyle這個(gè)參數(shù)還有另一個(gè)選擇:UIAlertControllerStyleActionSheet。選擇這個(gè)枚舉類(lèi)型后,實(shí)現(xiàn)效果如下:

發(fā)現(xiàn)這個(gè)提示框是從底部彈出的。是不是很簡(jiǎn)單呢?通過(guò)查看代碼還可以發(fā)現(xiàn),在提示框中的按鈕響應(yīng)不再需要delegate委托來(lái)實(shí)現(xiàn)了。直接使用addAction就可以在一個(gè)block中實(shí)現(xiàn)按鈕點(diǎn)擊,非常方便。

責(zé)任編輯:chenqingxiang 來(lái)源: 乞力馬扎羅的雪雪
相關(guān)推薦

2010-03-04 15:12:33

Python算法

2010-02-24 10:07:48

WCF跨越邊界

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學(xué)習(xí)UIKit Dynam

2015-09-25 09:44:24

ios9MapkitTrans

2015-08-20 09:00:23

ios9api

2015-07-02 17:32:28

iOS 9蘋(píng)果

2015-08-24 09:24:21

ios學(xué)習(xí)contacts fr

2010-06-09 09:34:11

2021-01-28 14:34:35

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2009-12-29 18:09:00

Silverlight

2010-03-04 11:12:02

Python AOP

2009-12-03 11:11:57

PHP網(wǎng)站優(yōu)化

2010-01-06 15:56:18

.Net Framew

2016-03-18 11:19:57

ios9replaykit入門(mén)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)