Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i have a loop that looks like this

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
271 views
Welcome To Ask or Share your Answers For Others

1 Answer

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...