Is there any way to prevent multiple button click events. In my application i have a scenario where multiple button events are getting called when the user taps the button continuously. Even after adding a loading indicator still sometimes its happening. Is there any generic solution to handle this ?
Fix multiple button click events in iOS?
2.1k Views Asked by subin272 At
3
There are 3 best solutions below
2

Try this if you using UiButton
@IBAction func tapButton(sender: Any) {
print("Tap")
let btn = sender as! UIButton
btn.isUserInteractionEnabled = false
}
Try this if you using UIBarButtonItem
leftButton = UIBarButtonItem(image: UIImage(named: "backimage")!, style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.toggleLeft))
leftButton.title = "Back"
navigationItem.leftBarButtonItem = leftButton
@objc public func toggleLeft() {
print("tap")
leftButton.isEnabled = false
// self.navigationController?.popViewController(animated: true)
}
After trying out different ways like enabling/disabling the button and enabling/disabling the UI (which is not at all recommended) the best way I feel is to use a "bool" variable to check the button clicked status.
Make sure you reset the bool value to false when navigating away from the
ViewController
. You can do that inViewDidDisappear
.