Reputation: 753
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 ?
Upvotes: 2
Views: 2428
Reputation: 753
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.
`
var isButtonTapped = false
@IBAction func tapButton(sender: Any) {
print("Tap")
if(!isButtonTapped){
//Perform your operations or page navigations. After the navigation operation set the bool value to true.
isButtonTapped = true;
}
}`
Make sure you reset the bool value to false when navigating away from the ViewController
. You can do that in ViewDidDisappear
.
Upvotes: 1
Reputation: 2778
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)
}
Upvotes: 1
Reputation: 335
when you click on the button in the click method put code
btnClick.userInteractionEnabled = false;
it will not let user to click button anymore after you are done with the task put another line of code to enable the user to click on the button
btnClick.userInteractionEnabled = true;
Upvotes: 1