Objective C - 循环更改标签文本 [英] Objective C - loop to change label text

查看:28
本文介绍了Objective C - 循环更改标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的循环

i have a loop that looks like this

for(int x=0; x < 10; x++){
    [testLabel setText:[self randomString]];
    sleep(1);
}

randomString 是一种从数组中返回随机字符串的方法.

The randomString is a method where it returns a random string from an array.

我正在使用睡眠,因此实际上可以看到标签被更改.

I'm using sleep so it is actually possible to see the label being changed.

循环工作正常,但标签仅在循环的最后一次迭代后更新.

The loop works fine but the label only gets updated after the last iteration from the loop.

有谁知道为什么会这样?有没有办法修复它?

Does anyone know why this could be? And would there be a way to fix it?

推荐答案

不要调用 sleep()

当然不会在主线程上使用,并且在辅助线程中使用 sleep 通常是非常值得怀疑的.

Do not call sleep()

Certainly not ever on the main thread and any use of sleep in secondary threads is generally highly questionable.

在这种情况下,只需使用 NSTimer 实例以定期更新值(如 Wilbur 所说).

In this case, just use an NSTimer instance to periodically update the value (as Wilbur said).

这篇关于Objective C - 循环更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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