Nazmul Hasan Nazmul Hasan - 2 months ago 6
Swift Question

"func prepareForSegue" not calling for CollectionView

Is It possible to use

shouldPerformSegueWithIdentifier
to catch the movement of segue with following method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
}


Here is my view controller code :

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let poke:Pokemon!

if inSearchMode {
poke=filterPokemons[indexPath.row]
} else {
poke=pokemonlist[indexPath.row]
}
shouldPerformSegueWithIdentifier("PokemonDetailVC", sender: poke);
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "PokemonDetailVC" {
if let detailVC = segue.destinationViewController as? PokemonDetailVC{
if let poke = sender as? Pokemon {
detailVC.pokemon=poke;
}
}
}
}


Storyboard segue reference

enter image description here

enter image description here

Answer

Try:

performSegueWithIdentifier("Identifier", sender: self)

Instead of:

shouldPerformSegueWithIdentifier("Identifier", sender: self)

When we use shouldPerformSegueWithIdentifier we can catch the movement of segue with following method:

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool

Check this link: Prevent segue in prepareForSegue method?