Swift 3 : Delegate within TapGestureRecognizer in

2020-04-20 06:31发布

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 ")
}

1条回答
beautiful°
2楼-- · 2020-04-20 07:04

You are never setting the submitAgeDelegate of the GenericController. Having a member of the same name in your MyCell 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 a UICollectionViewCell, you might use the reference that you make in tableView:cellForRowAtIndexPath: or similar.

查看更多
登录 后发表回答