解决Button多次点击重复执行方法的问题

NSInteger _currentClickNum; //Save the current value of the tag button is clicked

//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);
   
}
}
or
-(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.
}


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

字数256  阅读667  评论4  喜欢14

在日常开发中经常会碰到一种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;
}


如果大家有更好的解决办法,欢迎大家告诉我。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值