我创建了一个自定义集合视图单元格并以编程方式添加了一个按钮。但是没有调用按钮操作。下面是代码
//Button Creation
public var checkBoxButton: UIButton = {
let checkBoxButton = UIButton()
checkBoxButton.backgroundColor = .black
checkBoxButton.setImage(UIImage(named: "CheckBox_Normal"), for: .normal)
checkBoxButton.setImage(UIImage(named: "CheckBox_Selected"), for: .selected)
checkBoxButton.translatesAutoresizingMaskIntoConstraints = false
checkBoxButton.addTarget(self, action: #selector(paymentModeSelected(_:)), for: .touchUpInside)
return checkBoxButton
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(checkBoxButton)
}
@objc private func paymentModeSelected(_ sender:UIButton!){
sender.isSelected = true
print('Button clicked')
}
但是在init中写入按钮动作时会调用它 覆盖init(frame:CGRect){ super.init(frame:frame)
contentView.addSubview(checkBoxButton)
checkBoxButton.addTarget(self, action:
#selector(paymentModeSelected(_:)), for: .touchUpInside)
}