user2952122
user2952122

Reputation: 75

prepareForSegue UICollectionView not working (swift)

I’ve tried to push segue by using UICollectionView (show image form MasterView to DeatilView) but is not working. Xcode also doesn’t show any error message. What’s wrong with my code. I need some advice.

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

     if segue.identifier == "showDetail" {
      if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
         let detailVC = segue.destinationViewController as! DetailMenuViewController
             detailVC.picFood = self.collection[indexPath.row]

Upvotes: 2

Views: 2828

Answers (1)

Ashish Kakkad
Ashish Kakkad

Reputation: 23882

You need to add the UICollecitonViewDelegate

func collectionView(collection: UICollectionView, selectedItemIndex: NSIndexPath) 
{
   self.performSegueWithIdentifier("showDetail", sender: self)
}

After that add

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
          if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
             let detailVC = segue.destinationViewController as! DetailMenuViewController
                 detailVC.picFood = self.collection[indexPath.row]
          }
    }
}

Upvotes: 2

Related Questions