Retired Document
Important: This document is replaced by File System Programming Guide.
Using a Save Panel
Typically, you access an NSSavePanel
by invoking the savePanel
class method. A typical programmatic use of NSSavePanel requires you to:
Invoke
savePanel
Configure the panel (for instance, set its title or add a custom view)
Run the panel in a modal loop
Test the result; if successful, save the file under the chosen name and in the chosen directory
The following Objective-C code fragment demonstrates this sequence. (Two objects in this example, newView and textData, are assumed to be defined and created elsewhere.)
NSSavePanel *sp; |
int runResult; |
/* create or get the shared instance of NSSavePanel */ |
sp = [NSSavePanel savePanel]; |
/* set up new attributes */ |
[sp setAccessoryView:newView]; |
[sp setRequiredFileType:@"txt"]; |
/* display the NSSavePanel */ |
runResult = [sp runModal]; |
/* if successful, save file under designated name */ |
if (runResult == NSOKButton) { |
if (![textData writeToFile:[sp filename] atomically:YES]) |
NSBeep(); |
} |
When the class receives a savePanel
message, it tries to reuse an existing panel rather than create a new one. When a panel is reused its attributes are reset to the default values so the effect is the same as receiving a new panel. Because a Save panel may be reused, you shouldn't modify the instance returned by savePanel
except through the methods of the NSSavePanel
class. For example, you can set the panel’s title and required file type, but not the arrangement of the buttons within the panel.
Copyright © 2011 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2011-05-25