Avoid performSegue multiple times

295 Views Asked by At

I have more than 20+ buttons in my application. For these buttons if I click twice it performSegue twice and opens the viewcontroller twice, throughout my application in swift iOS?

self.performSegue(withIdentifier: "toViewController", sender: nil)
2

There are 2 best solutions below

1
Shehata Gamal On

You can attach a tag to each button and define an array of segues

self.performSegue(withIdentifier:segues[sender.tag], sender: nil)

or simply make the button the source of the segue , if you're not willing to override prepareForSegue

0
vadian On
  • You could declare a property

    var isSegueEnabled = true
    
  • Then implement the method to control whether the segue should be performed

    func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
        return isSegueEnabled
    }
    
  • In prepare(for segue set the property to false

    isSegueEnabled = false
    
  • At some point after the presented view controller has been dismissed set isSegueEnabled back to true