iOS Method for Preventing Multiple Responses Caused by Multiple Button Clicks

  • 2021-07-22 11:35:56
  • OfStack

iOS Method for Preventing Multiple Responses Caused by Multiple Button Clicks

In daily development, we often encounter one kind of bug because the user clicks a button quickly, which leads to repeated push on the page or repeated sending of network requests. Such problems not only have an impact on the user experience, but also increase the pressure on the server to a certain extent.

At present, I mainly use the following two methods to prevent buttons from clicking quickly

1. Cancel the previous operation every time you click (the method seen online)


- (void)buttonClicked:(id)sender
{
  // Here is the key. Click the button to cancel the previous operation before proceeding with the required operation 
  [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(buttonClicked:) object:sender];
  [self performSelector:@selector(buttonClicked: )withObject:sender afterDelay:0.2f];
}

2. After clicking, set the button to non-clickable state, and resume after a few seconds


-(void)buttonClicked:(id)sender{
  self.button.enabled = NO;
  [self performSelector:@selector(changeButtonStatus) withObject:nil afterDelay:1.0f];// Prevent users from clicking repeatedly 
}

-(void)changeButtonStatus{
  self.button.enabled = YES;
}

Or use:


- (void) timeEnough
{
 
UIButton *btn=(UIButton*)[self.view viewWithTag:33];
 
btn.selected=NO; 
[timer invalidate];
 
timer=nil; 
}

 - (void) btnDone:(UIButton*)btn
 {
 if(btn.selected) return;
 btn.selected=YES;
 [self performSelector:@selector(timeEnough) withObject:nil afterDelay:3.0]; // Use delay to limit. 
//to do something.
 } 

If you have a better solution, please leave a message.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: