Friday, August 27, 2010

How to add a delay of Timer between the execution of two lines of code in XCODE...

In this tutorial I am going to demenstrate how to add a Timer or delay between the execution of two lines of code. For example lets say you have one button and one UILABEL in a your xcode project. When the user clicks on the button it is to do the following:

1.)  send the variable dice to the UILABEL
2.) Two seconds later it will clear the text on the label.

This is how I have gotten this accomplished.

I have an IBACTION called randomNumber that I have linked to the button in my project.


-(IBAction)randomNumber {
int dice = 1 + arc4random() % (20);
label.text = [NSString stringWithFormat:@"%d", dice];
// perform selector after delay
[self performSelector:@selector(ClearLabel) withObject:nil afterDelay:2];

}

- (void)ClearLabel 

{
label.text = @"";
}

This code has been test in XCODE 3.2.3 and IPHONE SDK 4.00. 
If anyone has a better way of doing this please send an e-mail to timbojill@gmail.com. I do not read comments left behind by users

No comments:

Post a Comment