Nick Pitoniak
Nick Pitoniak

Reputation: 97

How to specify which view controller you are preparing for with Swift's prepareForSegue function?


So I have multiple buttons on one view segue'ing to other views. I have the function prepareForSegue() at the bottom of my code preparing one segue, however the app crashes when I use another segue on the view, even if it doesn't need any preparing.
I think that the problem is that all of the segues use the prepareForSegue() function, however the storyboard ID is different than the view it is transitioning to.
Is there any way to specify a prepareForSegue() function for each separate segue on the same view?

Code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let theVC: ViewController = segue.destinationViewController as! ViewController
    theVC.receivedString = "true"
}



Thanks

Upvotes: 0

Views: 97

Answers (1)

iAnurag
iAnurag

Reputation: 9346

In your prepareForSegue you should use a condition to check segue identifier like:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "yourSegueToAnyController")
        {
           //do code for specific viewController
        }

    }

Upvotes: 1

Related Questions