我有一个全局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;
}


通过在viewDidLoad上设置的NSTimer调用updateChecker。 setProgress:animated:和.progress均无效。我的进度栏几乎没有移动。所以我的问题是,您如何为从全局NSObject类调用的ASIHTTPRequest实施进度条?

最佳答案

您需要创建一个UIProgressView

UIProgressView *progressView = [UIProgressView alloc] init];


然后替换这个

  [request setDownloadProgressDelegate:self];


有了这个

[request setDownloadProgressDelegate:progressView];

关于ios - 使用在NSObject类上创建的ASIHTTPRequest添加下载进度栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19996481/

10-09 17:53