ios - 添加调用另一个类中的函数的点击手势
问题描述
我添加了UITapGestureRecognizer
一个元素。我希望在识别点击手势后调用的函数在另一个类中。
这是我的代码:
let selectMoment = SelectMomentClass(countUserMoment: 1, cardView: self.cardView)
selectMoment.draw()
let gesture = UITapGestureRecognizer(target: self, action: #selector(selectMoment.selectMomentAction))
cardView.addGestureRecognizer(gesture)
我SelectMomentClass
包含我的功能:
@objc func selectMomentAction(sender : UITapGestureRecognizer) {
print("card selectMoment is Tapped")
}
但是当我运行代码时,我遇到了这个错误:
无法识别的选择器发送到实例
我的代码哪里出错了?这是我需要更改的目标参数。我可以在另一个类中调用函数吗?
我在 Objective-C 中发现了一个类似的问题,但它对我没有帮助。
解决方案
使用这个。您的方法在selectMomentClass
类中定义,然后您必须将目标添加为selectMomentClass
对象,将选择器添加为#selector(selectMoment.SelectPictureAction)
。
let selectMoment = SelectMomentClass(countUserMoment: 1, cardView: self.cardView)
let gesture = UITapGestureRecognizer(target: selectMoment, action: #selector(selectMoment.SelectPictureAction(_:)))
selectMomentClass 中的方法签名如下。
@objc func selectPictureAction(_ sender : UITapGestureRecognizer) {
print("card selectMoment is Tapped")
}
推荐阅读
- python - 如何改进这个列表理解?
- r - 在 R 中创建自定义函数
- amazon-web-services - 如何通过 aws 代码管道将自定义柏树报告发送到 slack
- python - 如何使用 Nltk RegexTokenizer 获取文本的原始标记值
- javascript - 如何在css中将单词设置为垂直换行
- c# - .Net 核心 api 响应在小情况下如何更改默认设置
- c++ - 将 clangs 自定义 int 大小功能添加到 gcc 中吗?
- python - 通过 ubuntu 应用程序运行的 Matplotlib 弃用警告
- php - ng-init 在 angularjs 控制器末尾触发一个函数
- numbers - 无法使用 AppleScript 编辑数字文档