所以这是一个 Swift 中的 tvOS 项目。我有一个带有按钮的自定义 UICollectionViewCell 作为其子视图之一。我向按钮添加了一个目标,以允许它解释点击。这是相关代码的简化版本
class CustomCell: UICollectionViewCell {
var button:UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
button = UIButton(...) // Button is initialized with a frame
button.userInteractionEnabled = true
button.enabled = true
button.addTarget(self, action: "pressed:", forControlEvents: .PrimaryActionTriggered)
contentView.addSubview(button)
}
func pressed(sender: UIButton!) {
print("button pressed!")
}
}
出于某种原因,它从来没有打印出我的信息。我尝试将“pressedEnded”添加到单元格类中以查看它是否有任何内容并被调用
func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
// If I put a breakpoint here it's reached
}
有什么建议么?当我在模拟器中运行它时,按钮可以获得焦点,所以我不知道为什么它无法获得任何事件