Anokrize Anokrize - 1 year ago 148
iOS Question

Click inside swift 2.2 OSX

I am having multiple controls like NSTextfields and NSButtons. I couldn't find a solution for touch up inside events in swift 2.2. I tried it with the new selectors but I don't know how to handle touch events with them... Here is my code:

override func viewDidLoad() {
textfield.action = #selector(myAction)
button.action = #selector(myAction)

func myAction(sender: NSView)
print("aktuell: \(sender)")
currentObject = sender

I am not familiar with the new version of Swift 2.2 so I have no idea how to handle the touch events. I also tried to handle it over the delegate methods but this also didn't work because I have no method for the control if its being touched.

In the older versions of swift I had something like:

button.addTarget(self, action: Selector("actionMethod"), forControlEvents: UIControlEvents.TouchUpInside)

Error when trying to add the

Value of type 'NSButton' has no member 'addTarget'

But I can't find this in swift 2.2 and there is an error if I am trying to use it.
Thanks for help!

I am using OSX not iOS but this should be the same...

Answer Source

You need to set the NSButton's action. For example:

let button = NSButton() = self
button.action = #selector(someAction)

From NSButton Class Reference:

NSButton is a subclass of the NSControl class. An NSButton object sends an action message to a target object, such as a view controller, when the button is clicked.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download