我创建了一个 UICollectionView,其中包含名为 baseCell
的单元格。在这些单元格内部是另一个 UICollectionView,其中包含名为 subCell
的单元格。每个 subCell
中都有一个 UIButton。按钮有
这是设置按钮及其操作的代码:
let openOptionsButton: UIButton = {
let button = UIButton()
button.setImage(#imageLiteral(resourceName: "CircleAdd"), for: .normal)
button.addTarget(self, action: #selector(slideOverToOptions), for: .touchUpInside)
button.contentMode = .scaleAspectFit
return button
}()
func slideOverToOptions() {
print("This is working")
}
func setupViews(){
addSubview(openOptionsButton)
_ = openOptionsButton.anchor(topAnchor, left: nil, bottom: nil, right: rightAnchor, topConstant: 8, leftConstant: 0, bottomConstant: 0, rightConstant: 12, widthConstant: 48, heightConstant: 48)
}
当我按下按钮时,它不打印。我该如何解决这个问题?
最佳答案
我想通了这个问题。我的问题是我在我的按钮内部访问 self
(在 button.addTarget
行中),使用按钮的 let
声明。为了在我的按钮中访问 self
,我需要将 let
更改为 lazy var
。这解决了我的问题。
关于ios - 多个 UICollectionViewCells 内的 UIButton 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44895602/