当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS 防止按钮多次点击造成多次响应的方法

iOS 防止按钮多次点击造成多次响应的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

ios 防止按钮多次点击造成多次响应的方法

在日常开发中经常会碰到一种bug就是因为用户快速点击某个按钮,导致页面重复push或者重复发送网络请求。这样的问题既对用户体验有影响,而且还会一定程度上增加服务器的压力。

目前,我为了防止按钮快速点击主要使用以下两种办法

1.在每次点击时先取消之前的操作(网上看到的方法)

- (void)buttonclicked:(id)sender
{
  //这里是关键,点击按钮后先取消之前的操作,再进行需要进行的操作
  [[self class] cancelpreviousperformrequestswithtarget:self selector:@selector(buttonclicked:) object:sender];
  [self performselector:@selector(buttonclicked: )withobject:sender afterdelay:0.2f];
}

2.点击后将按钮置为不可点击状态,几秒后恢复

-(void)buttonclicked:(id)sender{
  self.button.enabled = no;
  [self performselector:@selector(changebuttonstatus) withobject:nil afterdelay:1.0f];//防止用户重复点击
}

-(void)changebuttonstatus{
  self.button.enabled = yes;
}

或者使用:

- (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]; //使用延时进行限制。
//to do something.
}

如果大家有更好的解决办法,欢迎大家留言说明。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网