Search code examples
iosobjective-cswiftwaitperformselector

How to Wait in Objective-C and Swift


I want to change my UILabel's text after 2 seconds.

I tried setting my UILabel's text to "A text", and use sleep(2) and finally changing the text to "Another text".

But sleep(2) only freezes the app and "Another text" is set without displaying "A text" for 2 seconds.

How may I display "A text" for 2 seconds and then show "Another text"?


Solution

  • You can use

    [self performSelector:@selector(changeText:) withObject:text afterDelay:2.0];
    

    or if you want to display it periodically, check the NSTimer class.