iOS9.0以后那些被不推荐使用(deprecated)方法之:sendAsynchronousRequest was deprecated in iOS 9、UIAlertView was depr

iOS9.0以后那些被不推荐使用(deprecated)方法之:sendAsynchronousRequest was deprecated in iOS 9、UIAlertView was deprecated

iOS9.0以后那些被不推荐使用(deprecated)方法之:sendAsynchronousRequest 和 UIAlertView 

一、UIAlertview

  在Xcode7 ,iOS9.0的SDK中,已经明确提示不再推荐使用UIAlertView,而只能使用UIAlertController;

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

   但是会有警告:“‘UIAlertView’ is deprecated:first deprecated in iOS 9.0 - UIAlertView is deprecated.。。。表明UIAlertView已经iOS9中被弃用(不推荐)使用。推荐使用UIAlertController。

  为解决这个warning,使用UIAlertController来解决这个问题。代码如下:

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

 

  通过运行发下,程序运行后的效果相同。 其中preferredStyle这个参数还有另一个选择:UIAlertControllerStyleActionSheet。选择这个枚举类型后,实现效果:提示框会从底部弹出。

  -》对比:通过查看代码还可以发现,在提示框中的按钮响应不再需要delegate委托来实现了。直接使用addAction就可以在一个block中实现按钮点击,非常方便。

 

二、NSURLSession替换NSURLConnection

 最近使用[NSURLConnection sendAsynchronousRequest]时已经警告为不推荐使用了,苹果官方推荐使用NSURLSession中的dataTaskWithRequest方法。

 用NSURLConnection实现的示例代码如下:

   推荐使用NSURLSession方法实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//推荐使用这种请求方法;
NSURLSession  *session = [ NSURLSession  sharedSession];
  
__block   NSString  *result = @;
NSURLSessionDataTask  *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^( NSData  * _Nullable data,  NSURLResponse  * _Nullable response,  NSError  * _Nullable error) {
    
   if  (!error) {
     //没有错误,返回正确;
     result = [[ NSString  alloc] initWithData:data encoding: NSUTF8StringEncoding ];
     NSLog (@返回正确:%@,result);
      
   } else {
     //出现错误;
     NSLog (@错误信息:%@,error);
   }
    
}];
  
  
[dataTask resume];

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值