Call a timer to notify you after a given time intercal:
[+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats][1]
Give it a simple method as the selector, which changes the image back to blue.
[blueButton setImage:[UIImage imageNamed:@"black.png"] forState:UIControlStateNormal];
[NSTimer timerWithTimeInterval:yourTimeInterval target:self selector:@selector(changeButtonBack:) userInfo:nil repeats:NO];
-(void) changeButtonBack:(id)sender{
[blueButton setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
}
Now this method will be called after whatever time interval you specify, and the button will go back to blue!
Note: After you make this call, your code continues executing. This is called setting up a call-back, because your code keeps on executing, and after the specified time, the system calls you back and tells you to execute the specified function.
If you want to have different behaviour in different places, you could either specify different selectors (read:methods to be called-back), or you could pass in an object of some sort as the userInfor
argument, which I set as nil
in the example. Then when you get called back, check the value of the userInfo
object and do what you want based on what it is.