Swift 3:在通配符中TapGestureRecognizer中的代理不被调用 [英] Swift 3 : Delegate within TapGestureRecognizer in Generics doesn't get called

查看:157
本文介绍了Swift 3:在通配符中TapGestureRecognizer中的代理不被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public protocol SubmitAgeDelegate:class {
func changeSubmitButtonBool()
}

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

 打开类GenericController< UICollectionViewCell,UICollectionReusableView> {

weak var submitAgeDelegate:SubmitAgeDelegate?

在UITapGestureRecognizer内

  func tapGestureDidRecognize(_ gesture:UITapGestureRecognizer){

如果让myAgeDelegate = self.submitAgeDelegate {
print(inside delegate)//从不进入
myAgeDelegate.changeSubmitButtonBool()
}

}

不太确定为什么它永远不会被叫?类似的方式在IBAction函数的常规类中也起作用。



在我的另一个类中:

 打开类MyCell:ActionCell, SubmitAgeDelegate {
weak var submitAgeDelegate:SubmitAgeDelegate?

public override init(frame:CGRect){
super.init(frame:frame)
submitAgeDelegate = self
initialize()
}

//委托
public func changeSubmitButtonBool(){

print(called)
}
/ pre>

解决方案

你永远不会设置 submitAgeDelegate c $ c> GenericController 。在您的 MyCell 类中具有相同名称的成员没有帮助。



为了设置其委托,您需要获得对 GenericController 的引用;没有办法。 (这与泛型没有任何关系;对于非泛型类,它是一样的)。由于它看起来像您使用它作为 UICollectionViewCell ,您可能使用您在 tableView中创建的引用:cellForRowAtIndexPath:或类似的。


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 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.

这篇关于Swift 3:在通配符中TapGestureRecognizer中的代理不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆