Disabling and delaying an IBAction button
I have an IBAction button that I would like to enable after a 30 second delay. the button would be in the view but disabled for 30 secs.
Does anyone know how I would go about doing this?
Here's what I have - a simple IBAction that plays some audio:
-(IBAction) playSound:(id)sender {
开发者_如何学Python [theAudio play];
}
You can use this:
- (IBAction)playSound:(id)sender
{
[theAudio play];
UIButton *theButton = (UIButton *) sender;
theButton.enabled = NO;
[self performSelector:@selector(enableButton:) withObject:theButton afterDelay:30.0];
}
- (void)enableButton:(UIButton *)button
{
button.enabled = YES;
}
Assuming that you want to disable the button when it gets pressed.
In viewDidLoad or other suitable method that you want:
[myButton setUserInteractionEnabled:FALSE]; [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(enableButton:) userInfo:nil repeats:NO];
Then,
- (void)enableButton:(NSTimer *)timer { [myButton setUserInteractionEnabled:TRUE]; }
NOTE : I have not compiled the code, just wrote. There might be typo.
精彩评论