Prepare for segue not passing data


Prepare for segue not passing data



I'm working on a Swift project using firebase and I have a UICollectionViewController that has all the categories, and I need to pass the category Id to the next view, but It passes an empty string


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let category = cnameArray[indexPath.row]
let catId = category.caId
func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "CatCity" {
let cityView = segue.destination as? CityViewController
cityView?.catId = catId!
} }
self.performSegue(withIdentifier: "CatCity", sender: self)

}



and in my CityViewController I have


var catId = String()



when I put a break point at the last line of the prepare func it gets hit and the console gets the value correctly while sitting a breakpoint at CityViewController gives me an empty var.
and I checked the code and my segue name and everything looks fine!





I dont think your prepare func should be inside the didselectIteAt.
– TheNitram
Jun 29 at 17:44




1 Answer
1



Correct setup


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let category = cnameArray[indexPath.row]
self.performSegue(withIdentifier: "CatCity", sender: category.caId)
}

func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "CatCity" {
if let cityView = segue.destination as? CityViewController {
cityView.catId = sender as! String
}
}
}





It's now working, thank you my dear.
– Sophie Bernard
Jun 29 at 18:27






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift