I have a protocol like so :
public protocol SubmitAgeDelegate : class {
func changeSubmitButtonBool()
}
The problem is I want to call it in a generics class.
open class GenericController<UICollectionViewCell,UICollectionReusableView> {
weak var submitAgeDelegate: SubmitAgeDelegate?
Within a UITapGestureRecognizer
func tapGestureDidRecognize(_ gesture: UITapGestureRecognizer) {
if let myAgeDelegate = self.submitAgeDelegate {
print("inside delegate") //Never gets inside
myAgeDelegate.changeSubmitButtonBool()
}
}
Not too sure why it never gets called? Similar ways have worked in regular classes withing IBAction functions.
In my other class :
open class MyCell: ActionCell, SubmitAgeDelegate {
weak var submitAgeDelegate: SubmitAgeDelegate?
public override init(frame: CGRect) {
super.init(frame: frame)
submitAgeDelegate = self
initialize()
}
// Delegate
public func changeSubmitButtonBool(){
print("called ")
}
You are never setting the
submitAgeDelegate
of theGenericController
. Having a member of the same name in yourMyCell
class doesn't help.You need to get a reference to your
GenericController
in order to set its delegate; there's no way around that. (This doesn't have anything to do with generics; it's the same for non-generic classes.) Since it looks like you're using it as aUICollectionViewCell
, you might use the reference that you make intableView:cellForRowAtIndexPath:
or similar.