views:

119

answers:

1

I have a simple document-based Cocoa app that acts as a viewer for .wav files, to do a bit of frequency analysis. I'd like to be able to export the data gleaned from opened files to CSVs for further analysis in other programs.

The document-based application framework in Cocoa lets you override

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError

to implement the default Save/Save As... workflow, but I don't want to write the files that I open.

The obvious thing to do is implement an export workflow in my document, to present a file save sheet, build some NSData, and write it to a file path, but there is not an obvious way to connect an outlet in the MainMenu nib to an action on a document controller.

So, then, what is the accepted way to implement such functionality in a document-based Cocoa app?

+3  A: 

You can create a new action in MainMenu.nib's "First Responder" object, called "export:", and connect to it. Then, implement an export: method in your document subclass. This will call your method.

The reason this works is that messages sent to the magic first responder object go through the entire responder chain, looking for some object that handles them. One of the items in that responder chain is the document, and so when the currently selected control, view, superview, window, etc, all do not handle the message, the document gets a chance. (The document controller is also on that chain, so you could use it, too.)

Take a look in Apple's responder chain docs - figure 1.10 covers this particular path.

Jesse Rusak
Ah! I knew I was missing something obvious. Thanks!
qwzybug