iOS 防止按鈕多次點擊造成多次響應的方法_IOS

來源:互聯網
上載者:User

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.}

如果大家有更好的解決辦法,歡迎大家留言說明。

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.