循环期间更新UIProgressBar进度 [英] updating UIProgressBar progress during loop

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

问题描述

我找不到关于如何在迭代循环的同时更新UIProgressbar进度的明确答案,例如:

I can't find a clear answer on how to update the progress of a UIProgressbar whilst iterating a loop e.g. :

for (int i=0;i<items.count;i++) {
    Object *new = [Object new];
    new.xxx = @"";
    new...
    ...
    float progress = (i+1) / (float)items.count;
    progressBar.progress = progress;
}
[self save];

如何在单独的线程上更新UI?

how can I update the UI on a seperate thread?

推荐答案

在后台线程上运行循环,并更新主线程上的进度条:

Run the loop on a background thread, and update the progress bar on the main thread:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    for (int i=0;i<items.count;i++) {
        Object *new = [Object new];
        new.xxx = @"";
        new...
        ...
        float progress = (i+1) / (float)items.count;
        dispatch_async(dispatch_get_main_queue(), ^{
            progressBar.progress = progress;
        });

    }
    [self save];
});

这篇关于循环期间更新UIProgressBar进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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