Bilal
Bilal

Reputation: 13

How to override the prepareForSegue with multiple Segue's

So I'm learning Core Data and I managed to create a segue with a parent relationship that allows me to segue to show only certain items based on the category you select in the previous VC (A Category View Controller). Now I have created a separate VC after this which shows the weather based on your location and to get to it you have to click the button from the original VC (The Category View Controller) to get to the weatherViewController. However every time I try to do so and perform a segue I run into this error below :

enter image description here

This is the code I have so far :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    let destinationVC = segue.destination as! NoteTableViewController
    let weatherDestinationVC = segue.destination as! WeatherViewController
    
    if let indexLocale = noteTableView.indexPathForSelectedRow {
        destinationVC.selectionCategory = noteArray[indexLocale.row]
        print(indexLocale.row)
    }   else {
        print("Error")
        weatherDestinationVC.delegate = self
        performSegue(withIdentifier: "goToWeather", sender: self)
    }
    
}


@IBAction func goToWeatherView(_ sender: UIBarButtonItem) {
    performSegue(withIdentifier: "goToWeather", sender: self)
}

Any help would be appreciated since I am just learning Core Data!

Upvotes: 0

Views: 439

Answers (1)

Frankenstein
Frankenstein

Reputation: 16381

Conditionally cast the different segue destination view controllers or check for the segue's identifier whichever you prefer.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destinationVC = segue.destination as? NoteTableViewController {
        if let indexLocale = noteTableView.indexPathForSelectedRow {
            destinationVC.selectionCategory = noteArray[indexLocale.row]
            print(indexLocale.row)
        }
    } else if let weatherDestinationVC = segue.destination as? WeatherViewController {
        weatherDestinationVC.delegate = self
    }
}

Also, you don't have to performSegue within prepareForSegue.

Upvotes: 2

Related Questions