Swift 3:在通配符中TapGestureRecognizer中的代理不被调用 [英] Swift 3 : Delegate within TapGestureRecognizer in Generics doesn't get called
问题描述
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 {
/ pre>
weak var submitAgeDelegate:SubmitAgeDelegate?
public override init(frame:CGRect){
super.init(frame:frame)
submitAgeDelegate = self
initialize()
}
//委托
public func changeSubmitButtonBool(){
print(called)
}
解决方案你永远不会设置
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 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.这篇关于Swift 3:在通配符中TapGestureRecognizer中的代理不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!