-1

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.

10
  • 1
    the code you posted has nothing to do with button. What are you trying to do ? May 6, 2016 at 12:33
  • stackoverflow.com/questions/30251663/… I found this code in here. Why are you telling this is nothing. I know that doesn't work. I'm asking because of that. Question title is very clear. So weird 3 downvote in 10 minutes.
    – jorjj
    May 6, 2016 at 12:40
  • So you have two buttons that each push a new viewController?
    – vacawama
    May 6, 2016 at 12:44
  • you need to provide more details of what you are trying to do and what the issue is! You can't just post one line of irrelevant code and say it didn't work.@yesman May 6, 2016 at 12:47
  • There are a number of answers to the linked question. Did you try setting the exclusiveTouch property to true for each of the buttons in the Storyboard or in viewDidLoad?
    – vacawama
    May 6, 2016 at 12:52

2 Answers 2

11

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.

2
  • thank you so much. It worked. Also for the nav bars : self.navigationController?.navigationBar.exclusiveTouch = true
    – jorjj
    May 6, 2016 at 14:00
  • 2
    extension UIButton { open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) self.isEnabled=false DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self.isEnabled = true } } } Jan 23, 2018 at 15:22
1

Your question is really not clear, but I suppose you're stuck with a very common problem which is solved by setting the property exclusiveTouch (see the docs) to true for all your buttons.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.