Iphone iOS-UIProgressView仅更新一次

Iphone iOS-UIProgressView仅更新一次,iphone,ios,cocoa-touch,uiprogressview,Iphone,Ios,Cocoa Touch,Uiprogressview,我正在从API加载数据,并使用UIProgressView显示加载了多少数据 在我的视图中,我将使用可达性检查是否存在internet连接。然后,如果有,则在函数中调用以下行10次 [self performSelectorInBackground:@selector(updateProgress) withObject:nil]; 然后运行此方法 -(void)updateProgress { float currentProgress = myProgressBar.progres

我正在从API加载数据,并使用UIProgressView显示加载了多少数据

在我的视图中,我将使用可达性检查是否存在internet连接。然后,如果有,则在函数中调用以下行10次

[self performSelectorInBackground:@selector(updateProgress) withObject:nil];
然后运行此方法

-(void)updateProgress {
    float currentProgress = myProgressBar.progress;
    NSLog(@"%0.2f", currentProgress);
    [loadingProg setProgress:currentProgress+0.1 animated:YES];
}
浮动增量为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
我有以下功能:

- (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
}

处理用户界面(UI)组件时,必须在主线程中执行这些方法。编程时,通常需要在主线程中设置UI操作,在后台线程中设置繁重、复杂和性能要求更高的操作-这称为多线程(作为附带建议,最好阅读GCD-Grand Central Dispatch。如果需要执行更长的操作,)

要解决这个问题,您应该调用
[self-performselectornmainthread:@selector(updateProgress)with-object:nil]然后,在方法中,执行以下操作:

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

UI刷新需要在主线程上进行。改变

[self performSelectorInBackground:@selector(updateProgress) withObject:nil];


啊,我明白了。这个观点可能需要比我预期的更多的工作!
[self-updateProgress]
的问题是,在加载所有内容之前,它不会执行任何操作。编辑答案,检查它。嗯,我尝试了
[self-performselectornmainthread:@selector(updateProgress)with-object:nil waitUntilDone:NO]但它在加载完成后再次工作。你认为我应该在后台执行加载,在主线程中执行UI吗?我不知道你说的加载是什么。如果您可以发布更多代码,将非常有用。请尝试以下操作:
dispatch_async(dispatch_get_main_queue(),^{[loadingProg setProgress:currentProgress+0.1 animated:YES];})您的问题中有一些我不明白的地方,您将根据从URL连接或设备磁盘读取的数据更新进度?您好,我尝试了这一行,但似乎不起作用。当我将waitUntilDone更改为YES时,NSLog会显示更新,但同样,进度条不会更改
[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];