Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With NSSavePanel, how can the user choose a specific file type to save?

I am writing an application in Cocoa which allows the user to export data in 3 different formats: CSV, JSON, and XML. I have added the allowed extensions to an NSSavePanel:

NSSavePanel* saveFile = [NSSavePanel savePanel];
NSArray* extensions = [[NSArray alloc] initWithObjects:@"csv", @"json" @"xml", nil];
[saveFile setAllowedFileTypes:extensions];

However, how can I set up the NSSavePanel to allow the user to select which format to save in, such as in TextEdit where a "File format" list box is offered? If this is possible, how would I then determine which format had been selected?

like image 267
BWHazel Avatar asked May 10 '12 12:05

BWHazel


People also ask

What is included in the nssavepanel?

The panel includes UI for browsing the file system, selecting a directory, and specifying the new name for the file. You can also add custom UI for your app using an accessory view. An NSSavePanel object reports user interactions to its associated delegate object, which must adopt the NSOpenSavePanelDelegate protocol.

What is the save panel used for?

The Save panel provides an interface for specifying the location to save a file and the name of that file. You present this panel when the user attempts to save a new document, or when the user saves a copy of an existing document to a new location.

What is the use of nssavepanel delegate?

An NSSavePanel object reports user interactions to its associated delegate object, which must adopt the NSOpenSavePanelDelegate protocol. Use your delegate object to validate the user's selection and respond to user interactions with the panel.

How do I add custom UI to a nssavepanel?

You can also add custom UI for your app using an accessory view. An NSSavePanel object reports user interactions to its associated delegate object, which must adopt the NSOpenSavePanelDelegate protocol. Use your delegate object to validate the user's selection and respond to user interactions with the panel.


1 Answers

You can add a user-defined NSView to the NSSavePanel using setAccessoryView:, see Apple's docs. There is also an Apple sample Custom Save. You add your format selection controls to this accessory view.

like image 175
CRD Avatar answered Oct 01 '22 05:10

CRD