myButton.addTarget(self, action: #selector(myButtonTapped), for: .touchUpInside) @objc func myButtonTapped() { print("My button was tapped!") }