Question

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 {

    [theAudio play];

}
Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top