swiftxcodecocoanstableviewfirst-responder

How to respond to "Delete" command from a macOS app in swift?


Let's say I created a NSWindowController which has a NSTableview with some elements. I now try to make it possible to use the "Edit -> Delete" Menu bar option which is set by default to "First Responder -> delete:". How do I implement this in my NSWindowController so my "delete" function in my NSWindowController is called and I can remove stuff (probably simply use the NSTableview selectedRows methods)?

I've tried to implement a function but the specific "Delete" element is disabled.

@objc func delete(sender: Any) {
    print("delete was pressed in the edit menu")
}

Solution

  • You have to declare the method (note the underscore character)

    @objc func delete(_ sender: Any) {
        print("delete was pressed in the edit menu")
    }