协议失败的数据传递

时间:2018-11-08 03:32:13

标签: swift delegates swift-protocols

嗨,我在使用自定义delegate传递数据时遇到问题,请帮助所有人,这是我的代码

protocol VideoCellDelegate {
    func didSaveFavorite(for cell: SearchVideoCell)
}

然后我将delegate放入SearchVideoCell

class SearchVideoCell: UICollectionViewCell {

var delegate: VideoCellDelegate?

这是我在SearchVideoCell内的功能

@objc func handleFavorite() {
    print("Handling favorite")
    delegate?.didSaveFavorite(for: self)
}

比我在SearchVideoController中传递的尝试

class SearchVideoController: UICollectionViewController, UICollectionViewDelegateFlowLayout, VideoCellDelegate

比我所说的delegate好,但它不起作用,只打印Handling Favorite而不是saving video to favorite

func didSaveFavorite(for cell: SearchVideoCell) {
    print("saving video to favorite")
}

2 个答案:

答案 0 :(得分:2)

在您的function shutdown() { if [[ "$?" != "0" ]] then # Only run if previous command was not successful npm run poststart fi } 中,您是否像下面那样分配了单元格的代表?

npmbash run dev

答案 1 :(得分:0)

制作单元时,您可能没有将控制器设置为委托对象。

//在您的控制器中

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath) as! SearchVideoCell
cell.delegate = self