I’m very very new to iOS programming but I already have a big issue I can’t solve. It seems so easy.
I have a button, I click on it to change a label called message
Here is the code:
- (IBAction)react:(id)sender {
int hasard ;
hasard=3;
message.text=[NSString stringWithFormat:@"1 %d",hasard];
sleep (1);
message.text=[NSString stringWithFormat:@"2 %d",hasard];
}
It works well but I don’t see the first message.text change.
When I click the button, I have to wait one second and I see 2 3
I thought I could see 1 3, wait a second and then see 2 3.
What is missing? It seems so obvious.
You are blocking the main thread (well, rather
sleep()blocks it). If you do so, committed changes to the UI won’t appear – you’ll only see the final result. You have to do something else (blocking the UI is a very bad idea in terms of user experience, by the way). You can try using a timer, for example: