iOS - UIProgressView只更新一次 [英] iOS - UIProgressView only updating once

查看:161
本文介绍了iOS - UIProgressView只更新一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个API加载数据,并使用UIProgressView来显示已加载了多少。



在我的viewWillAppear我使用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显示当前进程正在增加。但是,进度条仍为空。



更新:



p>

这是我如何调用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];

//这是我调用进度视图更新的地方

我有这些功能:

   - (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
{
//向可变数组和其他东西添加数据
}


解决方案

当您处理用户界面必须在主线程中执行方法。作为一个规则,当你编程时,你需要在主线程中设置UI操作,而在后台线程中需要繁重,复杂和性能要求较高的操作 - 这被称为多线程(作为一个建议,将有助于阅读关于GCD - Grand中央调度。如果您需要执行更长时间的操作,请检查这个很好的教程从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];
});
}


I am loading data from an API and am using a UIProgressView to display how much has loaded.

In my viewWillAppear I use Reachability to check that there is an internet connection. Then, if there is, the following line is called 10 times in a function.

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

This then runs this method

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

The float increments by 0.1 and the loading view displays this.

When the view is dismissed (it is a modal view) and then recalled, the method runs and the NSLog shows that the currentProgress is incrementing as it should. However, the progress bar remains empty. Does anyone know what could cause this?

For reference, I am using ARC.

Update:

This is how I am calling the 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

And I have these functions:

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

解决方案

When you are dealing with User Interface (UI) components, you must perform the methods in the main thread. As a rule when you program, you need to set UI operations in the main thread, and heavy, complex and more performance demanding operations in background threads - this is called multithreading (as a side suggestion, would be good to read about GCD - Grand Central Dispatch. If you need to do longer operations, check this good tutorial from Ray Wenderlich.)

To solve this, you should call [self performSelectorOnMainThread:@selector(updateProgress) withObject:nil]; and then, in the method, the following:

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

这篇关于iOS - UIProgressView只更新一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆