如何防止双击按钮

How to prevent double click on button

如何在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
  })

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

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

@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) 
    }
}