首先创建协议前加上@objc ,可选的方法前面加上@objc optional,必须实现的不需要加
@objc protocol PhotoPickerCollectionViewCellDelegate: NSObjectProtocol {
@objc optional func photoPickerCollectionViewCellAddPhotoBtnClick(cell: PhotoPickerCollectionViewCell)
}
class PhotoPickerCollectionViewCell: UICollectionViewCell {
weak var delegate: PhotoPickerCollectionViewCellDelegate?
@IBAction func addPhotoBtn(_ sender: Any) {
delegate?.photoPickerCollectionViewCellAddPhotoBtnClick?(cell: self)
}
}
实现协议
extension PhotoPickerCollectionViewController: PhotoPickerCollectionViewCellDelegate {
func photoPickerCollectionViewCellAddPhotoBtnClick(cell: PhotoPickerCollectionViewCell) {
print("")
}
}
最后别忘记设置代理,就这些吧!