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

1

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

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

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

Does anyone know how to do this?

    
asked by anonymous 28.06.2015 / 04:43

1 answer

0

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

@IBAction func sampleFunction(sender: AnyObject) {
}

Now, inside the square 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 XCode.

    
22.09.2016 / 19:44