Swift 3:Generics中TapGestureRecognizer中的委托不会被调用

时间:2016-12-17 03:51:50

标签: swift generics delegates

我有这样的协议:

public  protocol SubmitAgeDelegate : class  {
func changeSubmitButtonBool()
}

问题是我想在泛型类中调用它。

open class GenericController<UICollectionViewCell,UICollectionReusableView> {

weak var submitAgeDelegate: SubmitAgeDelegate? 

在UITapGestureRecognizer中

func tapGestureDidRecognize(_ gesture: UITapGestureRecognizer) {

    if let myAgeDelegate = self.submitAgeDelegate {
        print("inside delegate")   //Never gets inside
        myAgeDelegate.changeSubmitButtonBool() 
    }

}

不太确定为什么它永远不会被调用?类似的方法在具有IBAction功能的常规课程中起作用。

在我的另一堂课中:

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 个答案:

答案 0 :(得分:2)

您永远不会设置submitAgeDelegate的{​​{1}}。在GenericController课程中拥有相同名称的成员并没有帮助。

您需要获得对MyCell的引用才能设置其代理人;没有办法解决这个问题。 (这与泛型没有任何关系;对于非泛型类也是如此。)因为看起来你将它用作GenericController,你可以使用你在{{{1}中做出的引用。 1}}或类似的。