-2

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 addTarget function:

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...

Eiko
  • 25,601
  • 15
  • 56
  • 71
Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102

2 Answers2

1

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

let button = NSButton()
button.target = 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.

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
0

Swift2.2 deprecates using strings for selectors and instead introduces new syntax:#selector.

For example:

button.addTarget(self, action: #selector(actionMethod), forControlEvents: .TouchUpInside)

likumb
  • 9
  • 2