swift - 为什么我的自定义 UIButton 子类没有触发修饰 IBAction?
问题描述
这可能与我在自定义子类中覆盖 touchesBegan 的事实有关:
class myCustomButton: UIButton {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
print("TOUCHES BEGAN OVERRIDE CALLED")
originalColor = self.backgroundColor ?? .red
self.backgroundColor = .white
self.animateScale()
setNeedsDisplay()
}
//...
}
touchesEnded 类似地被覆盖,并且这两个函数都被调用并在按钮按下时将调试语句打印到控制台。但是,从我的视图控制器中按下的按钮 IBAction 中的代码永远不会运行
@IBAction func nextButtonPressed(_ sender: Any) {
print("this is never executed")
//...
}
我仔细检查了 IBOutlet 没有损坏,并将 (_ sender: Any) 更改为 (_ sender: AnyObject) 均无济于事。任何见解将不胜感激
解决方案
尝试覆盖 UIButton 的 sendAction 函数,而不是覆盖 touchesBegan:
override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
super.sendAction(action, to: target, for: event)
//Insert your code here
}
推荐阅读
- ios - Swift - 检查屏幕上的系统警报
- python - 在 conda 环境中为 NLTK 添加停用词资源
- javascript - javascript参数引用错误
- java - 如何将整数从地图保存到使用 for 循环
- apache-spark - 从 HDFS 源流式传输时如何运行多个批次?
- python-3.x - Scrapy为某些网络返回302
- android - 如何在通知频道中显示铃声选择器?
- ios - 如果用户离开 ios 应用程序,我可以在重新进入我的应用程序之前确定他们是否正在与另一个应用程序交互吗?
- angularjs - 如何保护 ASP.NET Core Web API 免受被盗 JWT 令牌以进行模拟
- javascript - 找到匹配项时循环遍历 JSON 对象并更新值的正确方法是什么