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 ?
Asked
Active
Viewed 1,918 times
2
-
Set UIView.exclusiveTouch? – Tuğberk Kaan Duman May 24 '18 at 13:03
-
did not get your question , can you place your code – Muhammad Shauket May 24 '18 at 13:06
-
@TuğberkKaanDuman exclusive touch wont work i guess. In my the issue is that multiple taps are detected and the code inside is executed more than once causing multiple navigations. – subin272 May 24 '18 at 13:07
-
@ShauketSheikh when i am tapping the button continuously multiple navigations are happening. I have added a loading indicator on button which covers the entire view so that user cant interact with UI. But still in some cases the button action executes more than once. – subin272 May 24 '18 at 13:10
-
btn.isUserInteractionEnabled = false when you tap on button – Muhammad Shauket May 24 '18 at 13:19
-
@ShauketSheikh Tried this in case of UIBarbutton item but it didnt work. – subin272 May 24 '18 at 13:20
3 Answers
1
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;

Nilay
- 327
- 3
- 9
1
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)
}

Muhammad Shauket
- 2,643
- 19
- 40
-
What to do in case of UIBarbuttonitem. It does not have “isUserInteractionenabled” property – subin272 May 24 '18 at 14:34
-
0
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
.