简体   繁体   中英

Custom accessory view for NSSavePanel of NSDocument

Consider a Cocoa NSDocument that supports two document types (eg BMP and JPEG). While one type has no save options (eg BMP), the other does (eg compression level for JPEG).

How do you implement this?

I understand that you should override prepareSavePanel: and set the accessory view of the given NSSavePanel . However, doing this replaces the default accessory view with the document type pop-up.

Is it necessary to recreate the document type pop-up if when using a custom accessory view?

If yes, how can pass the selected document type and the additional save options to the NSDocument write methods?

If no, how can I show the additional save options (eg compression level) only if the corresponding document (eg JPEG) type has been selected? Is there a delegate method for document type changes in the NSSavePanel ?

I'm pretty certain that yes, if you want a custom accessory view like this, you have to provide the entire thing. It's a shame — but there's no harm in filing a radar!

It might be possible to provide your own accessory view in -prepareSavePanel: and then override -fileTypeFromLastRunSavePanel to return the type selected. However, NSDocument 's docs make no promises that it'll respect that, annoyingly. Try it and see!

if that doesn't work, it looks like you need to provide your own implementation of -runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo: . Follow Apple's description of the method:

The default implementation of this method first makes sure that any editor registered using Cocoa Bindings' NSEditorRegistration informal protocol has committed its changes, then creates a save panel, adds a standard "file format" accessory view if there is more than one file type for the user to choose from and [self shouldRunSavePanelWithAccessoryView] returns YES, sets various attributes of the panel, invokes [self prepareSavePanel:theSavePanel] to provide an opportunity for customization, then presents the panel. If the user OKs the panel -saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo: is invoked.

The docs for -writableTypesForSaveOperation: do note:

You can invoke this method when creating a custom save panel accessory view to easily present the same set of types that NSDocument would in its standard file format popup menu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM