prepare segue - destination View Controller is called twice

375 Views Asked by At

In Xcode 13.1 I programmed the following code into my source view controller. It's working so well that it's calling the destination view controller twice (instead of once).

This is my code:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToNextViewControllerSegue" {
        let destVC = segue.destination as! DestinationViewController
        destVC.categoryTag = tag
    }
}

Any idea what I'm doing wrong?

Let me include the IBAction code as well (This doesn't seem to be the problem, though.)

@IBAction func startButtonTapped(_ sender: UIButton) {
    
    if tag == 1 || tag == 2 {
    self.performSegue(withIdentifier: "goToNextViewControllerSegue", sender: nil)
    } else {
        createAlert()
    }
}

Thanks for any help!

1

There are 1 best solutions below

5
jrturton On

Add a breakpoint to prepare(for segue: sender:) and look at the backtrace to see where it is being called from. My guess is that you have connected the button to the segue in the storyboard, and then also to the action method mentioned in your question, so the segue is being performed twice.