objective c - Calling sleep(5); and updating text field not working -


i'm trying sleep method (see below), rather textlabel changing value of mytextlabelstring, waiting 5 seconds, changing "sleep 5 worked", waiting 5 seconds , changing "sleep 5 worked second time round".... goes value of mytextlabelstring, waits 10 seconds, , changes "sleep 5 worked second time round".

- (void)textlabelchanger:(id)sender {      nsstring *mytextlabelstring = [nsstring stringwithformat:@"%d", gamecountdown];          textlabel.text=mytextlabelstring;     sleep(5);     textlabel.text=@"sleep 5 worked";     sleep(5);     textlabel.text=@"sleep 5 worked second time round";     return; } 

this provide result seek:

-(void)textlabelchanger:(id)sender {     nsstring *mytextlabelstring = [nsstring stringwithformat:@"%d", gamecountdown];         textlabel.text=mytextlabelstring;      [self performselector:@selector(updatetextlabelwithstring:) withobject:@"sleep 5 worked" afterdelay:5.0];     [self performselector:@selector(updatetextlabelwithstring:) withobject:@"sleep 5 worked second time round" afterdelay:10.0]; }  -(void)updatetextlabelwithstring:(nsstring*)thestring {     textlabel.text=thestring; } 

there plenty of ways this. instead of having single updatetextlabelwithstring call twice different delays, have dofirsttextupdate writes "sleep 5 worked" , calls selector dosecondtextupdate using same [self performselector:] technique after 5 second delay.

it's exceedingly rare you'll need use sleep() method objective-c.

-(void)textlabelchanger:(id)sender {     nsstring *mytextlabelstring = [nsstring stringwithformat:@"%d", gamecountdown];         textlabel.text=mytextlabelstring;      [self performselector:@selector(firstupdate) withobject:nil afterdelay:5.0]; } -(void)firstupdate {     textlabel.text = @"sleep 5 worked";     [self performselector:@selector(secondupdate) withobject:nil afterdelay:5.0]; } -(void)secondupdate {     textlabel.text = @"sleep 5 worked second time round"; } 

Comments