Invoke touchInside method via SWIFT programming

Asked

Viewed 186 times

0

Good evening guys, my doubt can be simple for many more I’m having a little trouble to solve 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.

Thanks in advance for your attention.

1 answer

2


Uibutton extends Uicontrol, which uses the concept of target/action for the treatment of events. Action is basically a method that will be triggered when the event occurs. Target is the object that receives the message, that is, that must implement the action.

In the example below I added a button in the storyboard and connected to the outlet. So in the method viewDidLoad defined the method to be called (buttonClicked - action), in the view controller instance (self - target). The last parameter is the type of event to be detected. In this 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")
    }
}
  • Thank you very much @Rafaelleao, that’s what I was looking for.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.