Owen Pierce
Owen Pierce

Reputation: 783

Temporarily Stop UIButton from responding

I have a button in my app that calls a long asynchronous function that we do not want to have called multiple times concurrently. However when I start mashing the button, it always sends as many messages as taps. I want to stop the button from responding while the function is running. I currently have a flag that stops execution of the function when the function is active in another thread, and the button is set to be hidden as soon as it enters this function, but these don't have any effect.

Thanks for any help!

Upvotes: 0

Views: 111

Answers (4)

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

Instead of thinking about disabling the button why not make the screen inactive. Show some message like "Processing..." or a Spinner. That way the user will know the something is happening & at the same time your problem is solved.

DSActivityView is a good library for this.

Upvotes: 1

Geekswordsman
Geekswordsman

Reputation: 1307

Assuming:

  1. Your button has an IBOutlet reference.
  2. You have an IBAction on the button.

Then you can simply set the button's Enabled property to NO, and re-enabled when you receive notification from your ASYNC task that its done.

-(IBAction) buttonClicked {
    [myButton setEnabled:NO];
    //Do stuff
}

-(void) notificationHandlerMethodForAsyncTaskDone:(NSNotification *)notification {
    [myButton setEnabled:YES];
    //Do stuff
}

Upvotes: 1

Rob
Rob

Reputation: 7707

You can use the enabled property to turn the button off:

[myButton setEnabled:NO]

Documentation

Upvotes: 0

Felipe Cypriano
Felipe Cypriano

Reputation: 2737

Inside the method that handle the touch event you can put disable the button:

- (void)handleTouch:(id)sender {
   // Do your asynchronous call
   [sender setEnabled:NO];
}

Upvotes: 1

Related Questions