-2

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

technerd
  • 14,144
  • 10
  • 61
  • 92
Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73

2 Answers2

2

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?

Community
  • 1
  • 1
Bista
  • 7,869
  • 3
  • 27
  • 55
2

You need to call performSegueWithIdentifier instead of shouldPerformSegueWithIdentifier because shouldPerformSegueWithIdentifier method gives you an opportunity to prevent a segue from happening. Returning NO from this method causes the segue to fail to perform.

If you want more detail about shouldPerformSegueWithIdentifier check this apple documentation

Where as performSegueWithIdentifier method allows to pass data from the source view controller to the destination view controller.

So now just call performSegueWithIdentifier like this instead of shouldPerformSegueWithIdentifier.

performSegueWithIdentifier("PokemonDetailVC", sender: poke)

In Swift 3.0

self.performSegue(withIdentifier: "PokemonDetailVC", sender: poke)
Nirav D
  • 71,513
  • 12
  • 161
  • 183