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

查看:92
本文介绍了目标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.

在这种情况下,只需使用

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

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

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