how to prevent double click on button in case of http request i've tried this code but in double click my request is sended twice to the server. please give me solution
@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
})
答案 0 :(得分: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)
}
}