• 异步加载网络数据,自定义进度条显示


     
     

    Objective-c代码 复制代码 收藏代码
    1. //调用方式 
    2.  
    3. Ajax *ajax = [[Ajax alloc]Ajax:urlStr  
    4.         target:self  
    5. didFinish:@selector(showData:)
    6.         isAllValues:NO  
    7.         valueForKey:@"list" 
    8.         showProgressBar:YES]; 
    9. [ajax release]; 
    10.  
    11. //异步回调方法 
    12. -(void) showData:(NSArray*)data{ 
    13.    NSLog(@"data:%@",data); 

    Objective-c代码 复制代码 收藏代码
    1. // 
    2. //  Ajax.h 
    3. //  live 
    4. // 
    5. //  Created by xjj xjj on 11-7-28
    6. //  Copyright 2011 新境界. All rights reserved. 
    7. // 
    8.  
    9. #import <Foundation/Foundation.h> 
    10. #import "UIProgressBar.h" 
    11.  
    12. @interface Ajax : NSObject { 
    13.  
    14. @property(nonatomic,assign) id target; 
    15. @property(nonatomic)SEL didFinish; 
    16. @property(nonatomic,retain) NSMutableData *buf; 
    17. @property(nonatomic,retain) NSURLConnection *connection; 
    18. @property(nonatomic,assign) BOOL isAllValues; 
    19. @property(nonatomic,assign) NSString* valueForKey; 
    20. @property(nonatomic,assign) BOOL isText; 
    21. @property(nonatomic,assign) NSString *urlStr; 
    22. @property(nonatomic,retain) UIProgressBar *progressBar; 
    23. @property(nonatomic,assign) long contentLength; 
    24. @property(nonatomic,assign) BOOL showProgressBar; 
    25.  
    26. -(void)start; 
    27. -(void)hiddenProgreesBar; 
    28.  
    29. /*! 
    30. @method Ajax:target:didFinish:isAllValues:valueForKey: 
    31. @discussion 异步加载某个节点数据(JSON格式) 
    32. @param _urlStr 网络数据URL 
    33. @param _target 调用者 
    34. @param _didFinish 数据加载完毕后通知动作 
    35. @param _isAllValues 是否返回所有数据 NO or YES 
    36. @param _valueForKey 加载某个节点数据 
    37. @result 初始化Ajax实例,并异步执行 
    38. */ 
    39. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey; 
    40.  
    41. /*! 
    42. @method Ajax:target:didFinish: 
    43. @discussion 异步加数据(文件本格式)所有文本数据 
    44. @param _urlStr 网络数据URL 
    45. @param _target 调用者 
    46. @param _didFinish 数据加载完毕后通知动作 
    47. @result 初始化Ajax实例,并异步执行 
    48. */ 
    49. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish; 
    50.  
    51. /*! 
    52. @method Ajax:target:didFinish:isAllValues:valueForKey:showProgressBar: 
    53. @discussion 异步加载某个节点数据(JSON格式) 
    54. @param _urlStr 网络数据URL 
    55. @param _target 调用者 
    56. @param _didFinish 数据加载完毕后通知动作 
    57. @param _isAllValues 是否返回所有数据 NO or YES 
    58. @param _valueForKey 加载某个节点数据 
    59. @param _showProgressBar 是否显示进度条 NO or YES 
    60. @result 初始化Ajax实例,并异步执行 
    61. */ 
    62. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey showProgressBar:(BOOL)_showProgressBar; 
    63.  
    64. /*! 
    65. @method Ajax:target:didFinish:showProgressBar 
    66. @discussion 异步加数据(文件本格式)所有文本数据 
    67. @param _urlStr 网络数据URL 
    68. @param _target 调用者 
    69. @param _didFinish 数据加载完毕后通知动作 
    70. @param _showProgressBar 是否显示进度条 NO or YES 
    71. @result 初始化Ajax实例,并异步执行 
    72. */ 
    73. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish showProgressBar:(BOOL)_showProgressBar; 
    74.  
    75. @end 

    实现

    Objective-c代码 复制代码 收藏代码
    1. // 
    2. //  Ajax.m 
    3. //  live 
    4. // 
    5. //  Created by xjj xjj on 11-7-28
    6. //  Copyright 2011 新境界. All rights reserved. 
    7. // 
    8.  
    9. #import "Ajax.h" 
    10. #import "JSONParser.h" 
    11. #import "UIProgressBar.h" 
    12.  
    13. @implementation Ajax 
    14. @synthesize target,didFinish; 
    15. @synthesize buf; 
    16. @synthesize connection; 
    17. @synthesize isAllValues,valueForKey; 
    18. @synthesize isText; 
    19. @synthesize urlStr; 
    20. @synthesize progressBar; 
    21. @synthesize contentLength; 
    22. @synthesize showProgressBar; 
    23.  
    24. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish showProgressBar:(BOOL)_showProgressBar{ 
    25.     if(self){ 
    26.         self.showProgressBar = _showProgressBar; 
    27.         self.urlStr = _urlStr; 
    28.         self.isText = YES; 
    29.         self.target = _target; 
    30.         self.didFinish = _didFinish; 
    31.         [self start]; 
    32.     } 
    33.     return self; 
    34. -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish{ 
    35.     /*self = [super init]; 
    36.     if(self){ 
    37.         self.showProgressBar = YES; 
    38.         self.urlStr = _urlStr; 
    39.         self.isText = YES; 
    40.         self.target = _target; 
    41.         self.didFinish = _didFinish; 
    42.         [self start]; 
    43.     } 
    44.     return self;*/ 
    45.     return [self Ajax:_urlStr target:_target didFinish:_didFinish showProgressBar:YES]; 
    46.  
    47. -(id) Ajax:(NSString *)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey showProgressBar:(BOOL)_showProgressBar{ 
    48.     self = [super init]; 
    49.     if(self){ 
    50.         self.showProgressBar = _showProgressBar; 
    51.         self.urlStr = _urlStr; 
    52.         self.isAllValues = _isAllValues; 
    53.         self.valueForKey = _valueForKey; 
    54.         self.target = _target; 
    55.         self.didFinish = _didFinish; 
    56.         [self start]; 
    57.     } 
    58.     return self; 
    59.  
    60. -(id) Ajax:(NSString *)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey{ 
    61.     return [self Ajax:_urlStr target:_target didFinish:_didFinish isAllValues:_isAllValues valueForKey:_valueForKey showProgressBar:YES]; 
    62.  
    63. -(void)start{ 
    64.     if(connection==nil){ 
    65.         [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    66.         buf = [[NSMutableData alloc] initWithLength:0]; 
    67.         NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
    68.         [request setURL:[NSURL URLWithString:urlStr]]; 
    69.         [request setHTTPMethod:@"GET"]; 
    70.         connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
    71.          
    72.         if([target isKindOfClass:[UIViewController class]]==YES&&showProgressBar==YES){ 
    73.             UIViewController *viewController = (UIViewController*)target; 
    74.             progressBar = [[UIProgressBar alloc] initWithFrame:CGRectMake(0, viewController.view.frame.size.height - 10,viewController.view.frame.size.width, 10)]; 
    75.             progressBar.minValue = 0
    76.              
    77.             [progressBar setLineColor:[UIColor blackColor]]; 
    78.             [progressBar setProgressColor:[UIColor redColor]];   
    79.             //[progressBar setProgressColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"3.png"]]];  
    80.             [progressBar setProgressRemainingColor:[UIColor greenColor]]; 
    81.             [viewController.view addSubview:progressBar];    
    82.         } 
    83.         [request release]; 
    84.     } 
    85. //收到响应时 
    86. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    87.     if(progressBar!=nil) 
    88.         progressBar.maxValue = response.expectedContentLength; 
    89. //接收数据 
    90. -(void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data{ 
    91.     [buf appendData:data]; 
    92.     if(progressBar!=nil) 
    93.         progressBar.currentValue += [data length]; 
    94. //加载失败 
    95. -(void)connection:(NSURLConnection*)aConn didFailWithError:(NSError*)error{ 
    96.     NSLog(@"didFailWithError:%@",error); 
    97.     [self hiddenProgreesBar]; 
    98. //接收完毕 
    99. -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    100.     if(didFinish!=nil){ 
    101.         if(isText) 
    102.             [target performSelector:didFinish withObject:buf]; 
    103.         else{ 
    104.             NSArray *data = [JSONParser parseJSON:buf isAllValues:isAllValues valueForKey:valueForKey]; 
    105.             [target performSelector:didFinish withObject:data]; 
    106.         } 
    107.     } 
    108.     [self hiddenProgreesBar]; 
    109. -(void) hiddenProgreesBar{ 
    110.     if(progressBar!=nil){ 
    111.     //淡淡消失效果 
    112.         [UIView animateWithDuration:2 
    113.                      animations:^{ 
    114.                          progressBar.alpha = 0
    115.                      }]; 
    116.         [progressBar performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2]; 
    117.     } 
    118.     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    119. -(void)dealloc{ 
    120.     [connection release]; 
    121.     [buf release]; 
    122.     [progressBar release]; 
    123.     [super dealloc]; 
    124. @end 
  • 相关阅读:
    ABP文档翻译--值对象
    (转)SqlServer 数据库同步的两种方式 (发布、订阅),主从数据库之间的同步
    Sql Service 的job作业新建过程
    Python --- pyinstaller --- 打包成exe并更换图标
    基于树形检测器的多标志识别
    记录 TypeError: render() got an unexpected keyword argument 'renderer' 错误
    Dockerfile 关键字
    Go new 和 make的区别
    Go的数据类型
    Mac Go 环境变量配置
  • 原文地址:https://www.cnblogs.com/jackljf/p/3589354.html
Copyright © 2020-2023  润新知