実装
let uButton = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
uBbutton.backgroundColor = .gray
uButton.tag = 0
uButton.addTarget(self, action: #selector(pressButton(_:)), for: .touchUpInside)
self.View.addSubview(uButton)
@objc func pressButton(_ button: UIButton) {
print("Button with tag: \(button.tag) clicked!")
}
参照
Stuck with adding target to button in swift 3 programmatically