异步加载网络数据,自定义进度条显示
原文地址:http://gekie.iteye.com/blog/1135627
- //调用方式
- Ajax *ajax = [[Ajax alloc]Ajax:urlStr
- target:self
- didFinish:@selector(showData:)
- isAllValues:NO
- valueForKey:@"list"
- showProgressBar:YES];
- [ajax release];
- //异步回调方法
- -(void) showData:(NSArray*)data{
- NSLog(@"data:%@",data);
- }
//调用方式 Ajax *ajax = [[Ajax alloc]Ajax:urlStr target:self didFinish:@selector(showData:) isAllValues:NO valueForKey:@"list" showProgressBar:YES]; [ajax release]; //异步回调方法 -(void) showData:(NSArray*)data{ NSLog(@"data:%@",data); }
- //
- // Ajax.h
- // live
- //
- // Created by xjj xjj on 11-7-28.
- // Copyright 2011 新境界. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "UIProgressBar.h"
- @interface Ajax : NSObject {
- }
- @property(nonatomic,assign) id target;
- @property(nonatomic)SEL didFinish;
- @property(nonatomic,retain) NSMutableData *buf;
- @property(nonatomic,retain) NSURLConnection *connection;
- @property(nonatomic,assign) BOOL isAllValues;
- @property(nonatomic,assign) NSString* valueForKey;
- @property(nonatomic,assign) BOOL isText;
- @property(nonatomic,assign) NSString *urlStr;
- @property(nonatomic,retain) UIProgressBar *progressBar;
- @property(nonatomic,assign) long contentLength;
- @property(nonatomic,assign) BOOL showProgressBar;
- -(void)start;
- -(void)hiddenProgreesBar;
- /*!
- @method Ajax:target:didFinish:isAllValues:valueForKey:
- @discussion 异步加载某个节点数据(JSON格式)
- @param _urlStr 网络数据URL
- @param _target 调用者
- @param _didFinish 数据加载完毕后通知动作
- @param _isAllValues 是否返回所有数据 NO or YES
- @param _valueForKey 加载某个节点数据
- @result 初始化Ajax实例,并异步执行
- */
- -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey;
- /*!
- @method Ajax:target:didFinish:
- @discussion 异步加数据(文件本格式)所有文本数据
- @param _urlStr 网络数据URL
- @param _target 调用者
- @param _didFinish 数据加载完毕后通知动作
- @result 初始化Ajax实例,并异步执行
- */
- -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish;
- /*!
- @method Ajax:target:didFinish:isAllValues:valueForKey:showProgressBar:
- @discussion 异步加载某个节点数据(JSON格式)
- @param _urlStr 网络数据URL
- @param _target 调用者
- @param _didFinish 数据加载完毕后通知动作
- @param _isAllValues 是否返回所有数据 NO or YES
- @param _valueForKey 加载某个节点数据
- @param _showProgressBar 是否显示进度条 NO or YES
- @result 初始化Ajax实例,并异步执行
- */
- -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey showProgressBar:(BOOL)_showProgressBar;
- /*!
- @method Ajax:target:didFinish:showProgressBar
- @discussion 异步加数据(文件本格式)所有文本数据
- @param _urlStr 网络数据URL
- @param _target 调用者
- @param _didFinish 数据加载完毕后通知动作
- @param _showProgressBar 是否显示进度条 NO or YES
- @result 初始化Ajax实例,并异步执行
- */
- -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish showProgressBar:(BOOL)_showProgressBar;
- @end
// // Ajax.h // live // // Created by xjj xjj on 11-7-28. // Copyright 2011 新境界. All rights reserved. // #import <Foundation/Foundation.h> #import "UIProgressBar.h" @interface Ajax : NSObject { } @property(nonatomic,assign) id target; @property(nonatomic)SEL didFinish; @property(nonatomic,retain) NSMutableData *buf; @property(nonatomic,retain) NSURLConnection *connection; @property(nonatomic,assign) BOOL isAllValues; @property(nonatomic,assign) NSString* valueForKey; @property(nonatomic,assign) BOOL isText; @property(nonatomic,assign) NSString *urlStr; @property(nonatomic,retain) UIProgressBar *progressBar; @property(nonatomic,assign) long contentLength; @property(nonatomic,assign) BOOL showProgressBar; -(void)start; -(void)hiddenProgreesBar; /*! @method Ajax:target:didFinish:isAllValues:valueForKey: @discussion 异步加载某个节点数据(JSON格式) @param _urlStr 网络数据URL @param _target 调用者 @param _didFinish 数据加载完毕后通知动作 @param _isAllValues 是否返回所有数据 NO or YES @param _valueForKey 加载某个节点数据 @result 初始化Ajax实例,并异步执行 */ -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey; /*! @method Ajax:target:didFinish: @discussion 异步加数据(文件本格式)所有文本数据 @param _urlStr 网络数据URL @param _target 调用者 @param _didFinish 数据加载完毕后通知动作 @result 初始化Ajax实例,并异步执行 */ -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish; /*! @method Ajax:target:didFinish:isAllValues:valueForKey:showProgressBar: @discussion 异步加载某个节点数据(JSON格式) @param _urlStr 网络数据URL @param _target 调用者 @param _didFinish 数据加载完毕后通知动作 @param _isAllValues 是否返回所有数据 NO or YES @param _valueForKey 加载某个节点数据 @param _showProgressBar 是否显示进度条 NO or YES @result 初始化Ajax实例,并异步执行 */ -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey showProgressBar:(BOOL)_showProgressBar; /*! @method Ajax:target:didFinish:showProgressBar @discussion 异步加数据(文件本格式)所有文本数据 @param _urlStr 网络数据URL @param _target 调用者 @param _didFinish 数据加载完毕后通知动作 @param _showProgressBar 是否显示进度条 NO or YES @result 初始化Ajax实例,并异步执行 */ -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish showProgressBar:(BOOL)_showProgressBar; @end
实现
- //
- // Ajax.m
- // live
- //
- // Created by xjj xjj on 11-7-28.
- // Copyright 2011 新境界. All rights reserved.
- //
- #import "Ajax.h"
- #import "JSONParser.h"
- #import "UIProgressBar.h"
- @implementation Ajax
- @synthesize target,didFinish;
- @synthesize buf;
- @synthesize connection;
- @synthesize isAllValues,valueForKey;
- @synthesize isText;
- @synthesize urlStr;
- @synthesize progressBar;
- @synthesize contentLength;
- @synthesize showProgressBar;
- -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish showProgressBar:(BOOL)_showProgressBar{
- if(self){
- self.showProgressBar = _showProgressBar;
- self.urlStr = _urlStr;
- self.isText = YES;
- self.target = _target;
- self.didFinish = _didFinish;
- [self start];
- }
- return self;
- }
- -(id) Ajax:(NSString*)_urlStr target:(id)_target didFinish:(SEL)_didFinish{
- /*self = [super init];
- if(self){
- self.showProgressBar = YES;
- self.urlStr = _urlStr;
- self.isText = YES;
- self.target = _target;
- self.didFinish = _didFinish;
- [self start];
- }
- return self;*/
- return [self Ajax:_urlStr target:_target didFinish:_didFinish showProgressBar:YES];
- }
- -(id) Ajax:(NSString *)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey showProgressBar:(BOOL)_showProgressBar{
- self = [super init];
- if(self){
- self.showProgressBar = _showProgressBar;
- self.urlStr = _urlStr;
- self.isAllValues = _isAllValues;
- self.valueForKey = _valueForKey;
- self.target = _target;
- self.didFinish = _didFinish;
- [self start];
- }
- return self;
- }
- -(id) Ajax:(NSString *)_urlStr target:(id)_target didFinish:(SEL)_didFinish isAllValues:(BOOL)_isAllValues valueForKey:(NSString *)_valueForKey{
- return [self Ajax:_urlStr target:_target didFinish:_didFinish isAllValues:_isAllValues valueForKey:_valueForKey showProgressBar:YES];
- }
- -(void)start{
- if(connection==nil){
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- buf = [[NSMutableData alloc] initWithLength:0];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
- [request setURL:[NSURL URLWithString:urlStr]];
- [request setHTTPMethod:@"GET"];
- connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
- if([target isKindOfClass:[UIViewController class]]==YES&&showProgressBar==YES){
- UIViewController *viewController = (UIViewController*)target;
- progressBar = [[UIProgressBar alloc] initWithFrame:CGRectMake(0, viewController.view.frame.size.height - 10,viewController.view.frame.size.width, 10)];
- progressBar.minValue = 0;
- [progressBar setLineColor:[UIColor blackColor]];
- [progressBar setProgressColor:[UIColor redColor]];
- //[progressBar setProgressColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"3.png"]]];
- [progressBar setProgressRemainingColor:[UIColor greenColor]];
- [viewController.view addSubview:progressBar];
- }
- [request release];
- }
- }
- //收到响应时
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
- if(progressBar!=nil)
- progressBar.maxValue = response.expectedContentLength;
- }
- //接收数据
- -(void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data{
- [buf appendData:data];
- if(progressBar!=nil)
- progressBar.currentValue += [data length];
- }
- //加载失败
- -(void)connection:(NSURLConnection*)aConn didFailWithError:(NSError*)error{
- NSLog(@"didFailWithError:%@",error);
- [self hiddenProgreesBar];
- }
- //接收完毕
- -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
- if(didFinish!=nil){
- if(isText)
- [target performSelector:didFinish withObject:buf];
- else{
- NSArray *data = [JSONParser parseJSON:buf isAllValues:isAllValues valueForKey:valueForKey];
- [target performSelector:didFinish withObject:data];
- }
- }
- [self hiddenProgreesBar];
- }
- -(void) hiddenProgreesBar{
- if(progressBar!=nil){
- //淡淡消失效果
- [UIView animateWithDuration:2
- animations:^{
- progressBar.alpha = 0;
- }];
- [progressBar performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];
- }
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- }
- -(void)dealloc{
- [connection release];
- [buf release];
- [progressBar release];
- [super dealloc];
- }
- @end