自iOS 5以来,setProgress不再更新UIProgressView [英] setProgress is no longer updating UIProgressView since iOS 5

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

问题描述

自iOS 5问世以来,我对进度条有点麻烦。下面的代码在iOS 5之前工作正常但是在iOS 5中,进度条不再显示在循环中设置的新进度。

I have a little trouble with a progress bar since iOS 5 came out. The code below was working fine before iOS 5 but with iOS 5 the progress bar is no longer displaying the new progress that is set within a loop.

代码预计会像这样工作:

The code is expected to work like this:


  1. 创建进度条(正常)

  2. 在新的后台流程中:设置初始进度0.25(工作)

  3. 在相同的后台进程中:通过循环更新进度(在iOS 4中工作)

以下是条形码init的代码:

Here's the code for the bar init:

// create a progress bar
UIProgressView *progressBar = [[UIProgressView alloc] initWithFrame:CGRectMake(coverSizeX*0.25, coverSizeY - 34.0, coverSizeX*0.5, 9.0)];
progressBar.progress = 0.0;
progressBar.progressViewStyle = UIProgressViewStyleBar;

在另一个线程中,它将进度的起点设置为0.25:

and in a different thread it sets a starting point for the progress to 0.25:

// set an initial progress
[progressBar setProgress: 0.25];

稍后它会在循环中更新进度以显示下载进度:

a little later it is updating the progress within a loop to display the download progress:

// within a for-loop:
NSNumber *counterPercentage;
for ( pageDownload = 1; pageDownload < pagesToDownload; pageDownload++ ) {
    counterPercentage = [[NSNumber alloc] initWithFloat: (float)pageDownload / (float)((float)pagesToDownload)];
    [progressBar setProgress: [counterPercentage floatValue]];
    [progressBar performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
}

...但屏幕上没有显示进度,进度条卡住了在设置的最初0.25进度。

… but the progress is not shown on the screen, the progress bar is stuck at the initial 0.25 progress that was set.

iOS 5版本是否有任何可能破坏它的变化?

Were there any changes with the iOS 5 release that could have broken it?

推荐答案

自iOS 5开关以来,我看过很多这样的问题,我不知道为什么只有iOS 5出现问题。但主要是因为我不确定之前为什么没有问题。

I've seen a lot of questions like this one since the iOS 5 switch, and I'm not sure why there is a problem only in iOS 5. But mainly because I'm not sure why there wasn't a problem before.

在你的代码中,你从后台线程调用 [progressBar setProgress:[counterPercentage floatValue]]; 。这是一个UI调用,不应该从后台线程进行。你也可以调用 setNeedsDisplay ,这不是更新 progressBar 所必需的,因为 UIProgressView 知道如何展示自己。 iOS 5似乎已经使更新UI的要求更加严格,但仅限于最佳实践。

In your code you call [progressBar setProgress: [counterPercentage floatValue]]; from a background thread. This is a UI call and should not be made from a background thread. Also you call setNeedsDisplay which is not necessary to update the progressBar since an UIProgressView knows how to display itself. iOS 5 seems to have made the requirements for updating the UI more stringent, but only to the point of what are best practices anyway.

在我看来,这看起来很完美用于块。使用块你的for循环可以这样写:

To my eye this looks like a perfect use for blocks. Using blocks your for loop could be written this way:

for ( pageDownload = 1; pageDownload < pagesToDownload; pageDownload++ ) {
        // Other stuff in background
    dispatch_async(dispatch_get_main_queue(), ^{
        progressBar.progress = ((float)pageDownload/(float)pagesToDownload);
    });
        // Other stuff in backgroud
}

这篇关于自iOS 5以来,setProgress不再更新UIProgressView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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