在Swift中自定义CollectionView Cell中未调用的按钮操作

时间:2017-11-13 00:37:01

标签: ios swift button action collectionview

我创建了一个自定义集合视图单元格并以编程方式添加了一个按钮。但是没有调用按钮操作。下面是代码

//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)


}

0 个答案:

没有答案
相关问题