如何防止双击按钮

问题描述 投票:-1回答:1

如何在http请求的情况下防止双击按钮我已经尝试过此代码但是在双击中我的请求被发送两次到服务器。请给我解决方案

 @IBAction func btnSendForgetPwdAction(_ sender: UIButton) {

  sender.isUserInteractionEnabled = false

  if checkIfFieldsAreFilledInforgetPwd(){
     self.showProgressView()
     var requestParams: Dictionary<String, String> = [:]
     requestParams["email"] = textFieldEmailForgetPWD.text

     let url = Constants.URLs.PREFIX_URL+Constants.URLs.FORGET_PASSWORD

     DataHelper.sharedInstance.alamofireManager().request(url, method: .post, parameters: requestParams, encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        self.hideProgressView()

        switch(response.result) {
        case .success(let JSON):
           if let response = JSON as? NSDictionary{
              print("JSON  \(response)")
              if   let isSuccess = response.object(forKey: "success") as? Bool{

              }
              // i have some code here


  Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { [weak sender] timer in

     sender?.isUserInteractionEnabled = true
  })
swift uibutton action
1个回答
0
投票

如果您正在进行异步Web服务调用,则需要等到完成后再重新启用按钮。不要使用计时器,因为您不知道完成呼叫需要多长时间。

没有看到你的服务器电话,我无法给出完整的答案,但你应该做这样的事......

@IBAction func btnBackAction(_ sender: UIButton) {

    sender.isUserInteractionEnabled = false

    Webservice().loadData { result in  // Your async server call 

        sender.isUserInteractionEnabled = true

        // Handle the result

       // performSegue goes here if you need to wait until your server call is complete  
       self.performSegue(withIdentifier: "HomePageViewController", sender: nil) 
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.