Fix multiple button click events in iOS?

Multi tool use
Fix multiple button click events in iOS?
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 ?
did not get your question , can you place your code
– Shauket Sheikh
May 24 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 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 at 13:10
btn.isUserInteractionEnabled = false when you tap on button
– Shauket Sheikh
May 24 at 13:19
3 Answers
3
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;
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)
}
What to do in case of UIBarbuttonitem. It does not have “isUserInteractionenabled” property
– subin272
May 24 at 14:34
i updated my answer
– Shauket Sheikh
May 24 at 14:58
Okay will check it out
– subin272
May 24 at 15:20
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 ViewDidDisapper.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Set UIView.exclusiveTouch?
– Tuğberk Kaan Duman
May 24 at 13:03