0

我有一个全局 NSObject 类,我在其中添加了一个 ASIHTTPRequest 函数来更新我的应用程序。我在 ViewController 类上设置了 UIProgressView。NSObject 类如下所示:

+(void)updateAllWithCategory:(NSMutableArray *)categories{
for(NSMutableArray *item in categories) {
    NSString *urlString = [NSString stringWithFormat:@"http://mywebsite.com/%@.xml", item];
    NSURL *url = [[NSURL alloc]initWithString:urlString];
    __block __weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setShowAccurateProgress:YES];
    [request setDownloadProgressDelegate:self];
    [request setTimeOutSeconds:20];
    [request setCompletionBlock:^{
        //download all data and show alert
    }];
    [request setFailedBlock:^{
        //display all errors
    }];
    [request startAsynchronous];
}}

float bytesReceived = 0;
float totalSize = 0;
float estimate = 0;

+(void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes {

bytesReceived = bytesReceived + (float)bytes;
float dlSize = (float)[request contentLength];
totalSize = totalSize + dlSize;

estimate = (bytesReceived / (totalSize * 1.0f) * 100);

//write estimate in plist with value estTime
}

然后在我的视图控制器上,我像这样使用上述功能:

-(void)updateChecker {
//get estTime from plist
[GlobalClass updateAllWithCategory:myArray]
progressbar.hidden = NO;
[progressbar setProgress:estTime animated: YES];
progressbar.progress = estTime;
}

updateChecker 通过在 viewDidLoad 上设置的 NSTimer 调用。setProgress:animated: 和 .progress 都不起作用。我的进度条一动不动。所以我的问题是,您如何为从全局 NSObject 类调用的 ASIHTTPRequest 实现进度条?

4

1 回答 1

2

您需要创建一个UIProgressView.

UIProgressView *progressView = [UIProgressView alloc] init];

然后替换这个

  [request setDownloadProgressDelegate:self];

有了这个

[request setDownloadProgressDelegate:progressView];
于 2013-11-15T10:26:49.157 回答