1

In my Cocoa application on the application menu there is a menu item "Delete", which sends a delete: message to first responder. Now, I have the keyboard focus on a NSOutlineView, which is the first responder in this case. Short of subclassing NSOutlineView, is there any way to handle the delete: message being passed to it? Neither the NSOutlineViewDelegate nor the NSOutlineViewDataSource protocol seems to define anything interesting for handling those actions.

Tony
  • 36,591
  • 10
  • 48
  • 83
  • What's your objection to subclassing? It's simple and will achieve what you want. – Rob Keniger Dec 26 '11 at 23:30
  • It certainly achieves the result, however handling those glue-elements seems to be the role of the controller instead of the view. Additionally, since Cocoa heavily advocates the delegate pattern I thought perhaps there is some built-in delegate for handling these tasks. – Tony Dec 26 '11 at 23:34

1 Answers1

3

The first responder is exactly that: The first responder. It will forward anything it isn't interested in to its next responder, and that may forward it in turn, and so on until the message reaches the application object.

So all you need to do to handle this message is be in the responder chain. A window controller is a good way to do it; it will be the window's next responder.

See “Event Architecture” in the Event-Handling Guide.

Tyler A.
  • 3,048
  • 1
  • 23
  • 27
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • 1
    From an architecture point of view, doesn't it seem messy to have the window controller handle "copy", "paste" and "delete" events that are local to the table/outline view? It seems that those methods are best handled by either the array/tree controller that the view binds to or the delegate of the view, but NSArrayController and NSTreeController are not NSResponders and can't be inside the responder chain, any suggestions? – Tony Dec 26 '11 at 20:54
  • 1
    You can always use custom `NSViewController` objects to manage each of your individual views and insert the view controllers in the responder chain. The view controllers can then handle copy and paste etc. – Rob Keniger Dec 26 '11 at 23:29
  • @Tony: Not if the window controller is or knows the object that owns the data (such as a document). Window controllers pretty much exist to intermediate between the views and window and the model-controller(s). It'd be best if the view handled cut, copy, paste, and delete itself (either through a binding or with data-source messages), but unless you want to make a subclass that adds that, it doesn't. – Peter Hosey Dec 26 '11 at 23:36
  • Got it. I ended up subclassing the view to forward those messages to the datasource. Seems to work pretty well and very elegant. Thanks Peter and Rob. – Tony Dec 26 '11 at 23:48