Invoke touchInside method via SWIFT programming

0

Good evening guys, my doubt can be simple for many more I'm having a bit of trouble solving the same. Come on!

I created a button (UIButton) and would like to know how I invoke an action when it is clicked via programming.

I thank you for your attention.

    
asked by anonymous 03.09.2015 / 04:19

1 answer

2

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")
    }
}
    
03.09.2015 / 14:47