UIProgressView没有更新? [英] UIProgressView not updating?

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

问题描述

我已经开始在iOS5中使用UIProgressView了,但还没有真正的运气。我无法更新视图。在每次更新进度后,我都有一系列顺序操作。问题是,进度视图不是一点一点地更新,而是仅在完成后才更新。它是这样的:

I have started playing with UIProgressView in iOS5, but havent really had luck with it. I am having trouble updating view. I have set of sequential actions, after each i update progress. Problem is, progress view is not updated little by little but only after all have finished. It goes something like this:

float cnt = 0.2;
for (Photo *photo in [Photo photos]) {
    [photos addObject:[photo createJSON]];
    [progressBar setProgress:cnt animated:YES];
    cnt += 0.2;
}

浏览堆栈溢出,我找到了这样的帖子 - setProgress不再更新自iOS 5以来的UIProgressView ,这意味着为了这个为了工作,我需要运行一个单独的线程。

Browsing stack overflow, i have found posts like these - setProgress is no longer updating UIProgressView since iOS 5, implying in order for this to work, i need to run a separate thread.

我想澄清这一点,我真的需要单独的线程让UIProgressView正常工作吗?

I would like to clarify this, do i really need separate thread for UIProgressView to work properly?

推荐答案

是的,进度视图的整个目的是用于线程化。

Yes the entire purpose of progress view is for threading.

如果你是在你正在阻止UI的主线程上运行该循环。如果您阻止UI,则用户可以进行交互,并且UI无法更新。你应该在后台线程上做所有繁重的工作并更新主线程上的UI。

If you're running that loop on the main thread you're blocking the UI. If you block the UI then users can interact and the UI can't update. YOu should do all heavy lifting on the background thread and update the UI on the main Thread.

Heres a little sample

Heres a little sample

- (void)viewDidLoad
{
    [super viewDidLoad];

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

}

- (void)backgroundProcess
{    
    for (int i = 0; i < 500; i++) {
        // Do Work...

        // Update UI
        [self performSelectorOnMainThread:@selector(setLoaderProgress:) withObject:[NSNumber numberWithFloat:i/500.0] waitUntilDone:NO];
    }
}

- (void)setLoaderProgress:(NSNumber *)number
{
    [progressView setProgress:number.floatValue animated:YES];
}

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

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