ios - 多个 UICollectionViewCells 内的 UIButton 无法正常工作

标签 ios swift swift3 uibutton

我创建了一个 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/

相关文章:

ios - 使用 WebRTC 在 native iOS 应用程序中共享屏幕

objective-c - 苹果手机 : responder chain in UIScrollView

ios - Navigation Controller 中 ViewController 和 ViewController 的两个 segue

ios - AppDelegate 发送响应 swift 3

ios - findOrCreate 在 Swift 3 中没有硬编码 entityName

ios - 使用 [UIView animateKeyframesWithDuration] 为 UIView 设置动画的奇怪行为

iphone - 如何在 UITextField 的开头添加空格

ios-charts - 通过拖放更改 Y 轴值

ios - 获取更大的Facebook好友头像

ios - 无法使用 Developer Authentication Swift Cognito 承担经过身份验证的角色