How to prevent a multiple event on the same UIButton in iOS?

I want to prevent continuous multiple clicks on the same UIButton .

I tried using the enabled and exclusiveTouch properties, but that didn't work. For example:

 -(IBAction) buttonClick:(id)sender{ button.enabled = false; [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{ // code to execute } completion:^(BOOL finished){ // code to execute }]; button.enabled = true; } 
+8
ios uibutton touch
source share
3 answers

What you do, you just set on / off outside the block. It is wrong to execute it after calling this method, so its not turning off the button until the completion block is called. Instead, you should reuse it after the animation is complete.

 -(IBAction) buttonClick:(id)sender{ button.enabled = false; [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{ // code to execute } completion:^(BOOL finished){ // code to execute button.enabled = true; //This is correct. }]; //button.enabled = true; //This is wrong. } 

Oh, yes, instead of true and false , YES and NO look good. :)

+13
source share

This is my decision:

NSInteger _currentClickNum; // The current value of the tag button is saved by clicking

 //Button click event - (void)tabBt1nClicked:(UIButton *)sender { NSInteger index = sender.tag; if (index == _currentClickNum) { NSLog(@"Click on the selected current topic, not execution method, avoiding duplicate clicks"); }else { [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(tabBtnClicked:) object:sender]; sender.enabled = NO; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ sender.enabled = YES; }); _currentClickNum = index; NSLog(@"Column is the current click:%ld",_currentClickNum); } } 
0
source share

In my case, installing isEnabled was not fast enough to prevent multiple taps. I had to use the property and the guard to prevent multiple taps. And the action method calls a delegate who usually rejects the view controller, but with a few keystrokes that it doesn't reject. dismiss(...) should cancel itself if the code is still running on the view controller, not sure. Despite this, I had to add the dismiss manual to the dismiss .

Here is my solution ...

 private var didAlreadyTapDone = false private var didNotAlreadyTapDone: Bool {return !didAlreadyTapDone} func done() { guard didNotAlreadyTapDone else { self.dismiss(animated: true, completion: nil) return } didAlreadyTapDone = true self.delegate.didChooseName(name) } 
0
source share

All Articles