Search code examples
xcodeswiftuibuttonmulti-touch

How can I disable Multi Click for UIButton in Swift?


I have two buttons that have their own segue with identifiers. I want to disable the multiple clicks for these buttons so both cannot be clickable at the same time.

My code :

    self.view.multipleTouchEnabled = false
    self.view.exclusiveTouch = true

I tried this code in viewDidLoad but didn't work.


Solution

  • Add @IBOutlets for each of your buttons by control-dragging from each button to the viewController and give them unique names such as button1, button2 and button3.

    class ViewController: UIViewController {
    
        @IBOutlet weak var button1: UIButton!
        @IBOutlet weak var button2: UIButton!
        @IBOutlet weak var button3: UIButton!
    

    Then in viewDidLoad, set the exclusiveTouch property to true for each of the buttons:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        button1.exclusiveTouch = true
        button2.exclusiveTouch = true
        button3.exclusiveTouch = true
    

    This will prevent a second button from being pressed while the pressing of the first one is in process.