iOS - UIProgressView只更新一次

Pat*_*ick 3 iphone cocoa-touch uiprogressview ios

我正在从API加载数据,并使用UIProgressView来显示已加载的数量.

在我的viewWillAppear中,我使用Reachability检查是否存在互联网连接.然后,如果有,则在函数中调用以下行10次.

[self performSelectorInBackground:@selector(updateProgress) withObject:nil];
Run Code Online (Sandbox Code Playgroud)

然后运行此方法

-(void)updateProgress {
    float currentProgress = myProgressBar.progress;
    NSLog(@"%0.2f", currentProgress);
    [loadingProg setProgress:currentProgress+0.1 animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

浮点数递增0.1,加载视图显示此值.

当视图被解除(它是模态视图)然后被调用时,该方法运行并且NSLog显示currentProgress正在递增.但是,进度条仍为空.有谁知道是什么原因引起的?

作为参考,我正在使用ARC.

更新:

这就是我调用API的方式

NSString *urlString = **url**;
NSURL *JSONURL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:JSONURL
                        cachePolicy:NSURLRequestReloadIgnoringCacheData 
                        timeoutInterval:10];
if(connectionInProgress) {
    [connectionInProgress cancel];
}
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

//This is where I call the update to the progress view
Run Code Online (Sandbox Code Playgroud)

我有这些功能:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    JSONData = [NSMutableData data];
    [JSONData setLength:0];
}

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [JSONData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    //add data to mutable array and other things
}
Run Code Online (Sandbox Code Playgroud)

Nat*_* R. 7

在处理用户界面(UI)组件时,必须在主线程中执行方法.通常在编程时,需要在主线程中设置UI操作,在后台线程中设置繁重,复杂且性能要求较高的操作 - 这称为多线程(作为一个侧面建议,很适合阅读GCD - Grand Central Dispatch.如果你需要做更长时间的操作,请查看Ray Wenderlich的这篇优秀教程.)

要解决此问题,您应该调用[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil];,然后在方法中调用以下内容:

-(void)updateProgress {
    float currentProgress = myProgressBar.progress;
    NSLog(@"%0.2f", currentProgress);
    dispatch_async(dispatch_get_main_queue(), ^{
    [loadingProg setProgress:currentProgress+0.1 animated:YES];
    });
}
Run Code Online (Sandbox Code Playgroud)