swift - collectionViewCell 中的按钮不触发功能
问题描述
我在补充 collectionView 单元格中创建了一个按钮作为标题,并向其添加了一个带有目标的按钮,但点击时它不会触发该功能。我究竟做错了什么?
下面是我在单元格类中创建的按钮及其目标函数。
let dummyButton :UIButton = {
let button = UIButton(type: .system)
button.setTitle("Dummy", for: .normal)
button.layer.cornerRadius = 3
button.layer.borderWidth = 1
button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
button.tintColor = UIColor.brown
button.addTarget(self, action: #selector(handleTrash), for: .touchUpInside)
return button
}()
@objc func handleTrash (){
print("It worked this time around so what was going on?")
}
我在 collectionView Cell 子类中编写了所有这些。请帮忙
解决方案
我找到了解决方案。我必须将按钮声明为惰性变量。
推荐阅读
- python - PyCharm Python 日志记录与提示器刷新竞赛?
- c - 有没有办法在 Linux 上使用 GCC/Clang 强制多重定义的符号?
- design-patterns - 如何定义抽象小部件?
- python - Pytorch 将 LSTM 模型转换为数据并行以供多 GPU 使用
- spring - 如何在 java 配置中使用 spring 5.1.x 的预认证机制获取 Websphere LTPA SSO 令牌?
- sas - 需要从 SAS 数据集在 Yellowbrick 数据库中创建一个表
- python - 如何在不使用列表的情况下将变量的值用作新变量?
- android - 通知未在使用广播接收器的服务中触发
- nearprotocol - 处理来自 nearlib 的账户和交易签名
- sql - 多个日期的最新记录