gpt4 book ai didi

ios - 在循环期间更新 UIProgressBar 进度

转载 作者:行者123 更新时间:2023-11-29 02:49:47 27 4
gpt4 key购买 nike

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

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?

最佳答案

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

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];
});

关于ios - 在循环期间更新 UIProgressBar 进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24701643/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com