prepareForSegue UICollectionView 不工作 (swift)

prepareForSegue UICollectionView not working (swift)

我尝试使用 UICollectionView 推送 segue(将图像从 MasterView 显示到 DeatilView),但没有用。 Xcode 也没有显示任何错误信息。我的代码有什么问题。我需要一些建议。

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]

您需要添加 UICollecitonViewDelegate

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

之后添加

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]
          }
    }
}