setProgress is no longer updating UIProgressView since iOS 5

前端 未结 2 1123
执笔经年
执笔经年 2020-12-11 03:52

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 ne

2条回答
  •  误落风尘
    2020-12-11 04:23

    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.

    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.

    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
    }
    

提交回复
热议问题