3

My NSDocument subclass (MyDocument) is quite large and I was thinking about making a MyDocumentResponder (subclass of NSResponder) to handle document responder events in order to clean up the code. This seems better to me than a category on MyDocument.

MyDocumentResponder would be instantiated in the MyDocument nib and would have a property that hooks it to its owning MyDocument.

Within MyDocumentResponder, I can implement acceptsFirstResponder, but how do I correctly hook it into the responder chain (and remove it if the document loses its front-most status?

Should I hook it in by adding becomeFirstResponder and resignFirstResponder to MyDocument? In this case, should it go in front of or behind MyDocument?

Trygve
  • 1,317
  • 10
  • 27
  • What are "document responder events"? It sounds like you implemented everything in your document class instead of the window controller or view controller. – Willeke Nov 07 '16 at 15:24
  • My document has a sourceView, several tableViews and a few other views. I have a menu command to create an item in the sourceView (which always needs to be available), but if a tableView is the First Responder, then the sourceView is not in the responder chain (but of course the document is). I believe this is because the sourceView and tableViews are siblings at the same view-level. So the document needs to capture the "create item" action and forward it to the sourceView. – Trygve Nov 07 '16 at 16:06
  • The "create item" action can be handled by the window controller or the view controller. – Willeke Nov 07 '16 at 17:00
  • Which is really no different to handling it in the document controller as it is now (since I do not override makeWindowControllers) – Trygve Nov 07 '16 at 17:09
  • Create a subclass of NSWindowController or NSViewController instead of MyDocumentResponder. The window controller is in the responder chain. See [The Classes That Support Document-Based Apps](https://developer.apple.com/library/prerelease/content/documentation/DataManagement/Conceptual/DocBasedAppProgrammingGuideForOSX/KeyObjects/KeyObjects.html#//apple_ref/doc/uid/TP40011179-CH3-SW8) – Willeke Nov 07 '16 at 22:27

0 Answers0