How to simulate a button click on Swift (for MAC OSX)

Asked

Viewed 124 times

2

I’m making a program for Mac and need to find a way to "click" a button from a code.

I’m trying to put this code on another button and I’ve tried it in the 3 following ways:

func mouseDown(_ NSButton1: NSEvent) { }
func performClick(_ NSButton1: NSEvent) { }
Button1.sendActionsForControlEvents(NSControlEvents.TouchUpInside)

Someone knows how to do it?

  • 1

    Welcome to Stack Overflow! Could you edit your question and post together the code of what you have tried? Also take the opportunity to take a look at the page How To Ask that will help you build your question better

1 answer

0

Hello! If you want to define what will happen with a button after you click it, just connect the Storyboard button to the code, choose the type of Connection as Action. Thus, a new function will be created, for example:

@IBAction func sampleFunction(sender: AnyObject) {
}

Now, inside the brackets, write the actions you want. For example:

@IBAction func sampleFunction(sender: AnyObject) {
    print("Você clicou em mim!!")
}

In this case, every time you click the button, the string "You clicked on me!" will be "printed" in the Xcode.

Browser other questions tagged

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