UIButton extends UIControl, which uses the target / action concept to handle events. Action is basically a method that will be triggered when the event occurs. Target is the object that receives the message, that is, it should implement the action.
In the example below I added a button on the storyboard and connected to the outlet. Then in the viewDidLoad
method I define the method to be called (buttonClicked - action ), in the instance of the view controller (self - target ). The last parameter is the type of event to be detected. In case I used TouchUpInside, which is the regular click.
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
}
func buttonClicked(sender:UIButton) {
println("Botão pressionado")
}
}