-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.

jorjj
  • 1,479
  • 4
  • 20
  • 36
  • 1
    the code you posted has nothing to do with button. What are you trying to do ? – Teja Nandamuri May 06 '16 at 12:33
  • http://stackoverflow.com/questions/30251663/how-to-disable-multi-click-on-button 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 06 '16 at 12:40
  • So you have two buttons that each push a new viewController? – vacawama May 06 '16 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 – Teja Nandamuri May 06 '16 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 06 '16 at 12:52
  • Ok I added some more clear explain and code. – jorjj May 06 '16 at 12:52
  • @vacawama yes, I tried but didn't work. I'm clicking 3 buttons, all of them working and opening all pages one by one. – jorjj May 06 '16 at 12:55
  • You should be setting `exclusiveTouch` to `true` for each of your buttons. Are you doing that, and where? – vacawama May 06 '16 at 12:56
  • some buttons have triggered segues. I tried these codes on `viewDidLoad`. So I create event for this buttons and added this code. Am I truly understand? – jorjj May 06 '16 at 13:04
  • @yesman, I added code to do what you want. Give that a try. – vacawama May 06 '16 at 13:48

2 Answers2

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.

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • thank you so much. It worked. Also for the nav bars : `self.navigationController?.navigationBar.exclusiveTouch = true` – jorjj May 06 '16 at 14:00
  • 2
    extension UIButton { open override func touchesBegan(_ touches: Set, with event: UIEvent?) { super.touchesBegan(touches, with: event) self.isEnabled=false DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self.isEnabled = true } } } – Rakesh Yembaram Jan 23 '18 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.

werediver
  • 4,667
  • 1
  • 29
  • 49