在for()循环的中间更新UILabel [英] Updating UILabel in the middle of a for() loop

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

问题描述

我有一个带有for()循环的方法.在该循环中,mylabel.text被多次更新.但是,实际的标签只有在方法完成后才在屏幕上更新,并使用for()循环中创建的最后一个值进行更新.

I have a method with a for() loop. In that loop, mylabel.text is updated many times. However, the actual label does not update on the screen until the method is done, updating with the last value created in the for() loop.

使用NSLog,它确实在for()循环的中间进行更新,我发现标签的值确实发生了很多次更改.

Using an NSLog, which does update in the middle of the for() loop, I see the value indeed changing for the label many times.

iOS中的常规做法是不更新for()循环中间的标签吗?我想有办法做到这一点.

Is it the general practice in iOS to not update labels in the middle of the for() loop? I would imagine there is a way to do this.

推荐答案

您可以通过告诉run循环像这样运行来更新UI:

You can make the UI update by telling the run loop to run like this:

for (NSInteger i = 0; i < 10; i++) {
    [label setText:...];
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
}

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

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