Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Label Text Change with sleep()

Tags:

ios

I found interesting things.. Following code doesn't show @"One" and it show @"Two" after 3 seconds delay.. I think that @"One" need to be shown and then 3 seconds delay and then @"Two" need to pop up.. Am I wrong?

self.statusLabel.text = @"One";
sleep(3);
self.statusLabel.text = @"Two";

Thanks..

like image 826
MomentH Avatar asked Dec 28 '22 15:12

MomentH


1 Answers

If you're doing this on the main thread, that sleep(3) will block it, freezing the app for 3 seconds. Event processing, including things like repainting the UI, won't happen til that's over.

To get what you're expecting, try something like this:

[self.statusLabel setText:@"One"];
[self.statusLabel performSelector:@selector(setText:)
                       withObject:@"Two"
                       afterDelay:3.0];

Does the first change, then queues up an invocation performing the second change to happen in the future. Then returns control to the OS to do any necessary redrawing.

like image 108
rgeorge Avatar answered Jan 11 '23 23:01

rgeorge