我正在从 API 加载数据,并使用 UIProgressView 来显示已加载的数据量。
在我看来,WillAppear 我使用 Reachability 来检查是否有互联网连接。然后,如果存在,则在函数中调用以下行 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。
Update:
这就是我调用 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集中调度。如果您需要进行较长时间的操作,查看 Ray Wenderlich 的这个好教程 http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial.)
要解决这个问题,你应该打电话[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];
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)