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 runModalForDirectory:NSHomeDirectory() file:@""]; |
/* 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 listed below. For example, you can set the panel’s title and required file type, but not the arrangement of the buttons within the panel. If you must modify the Save panel substantially, create and manage your own instance using the alloc...
and init...
methods rather than the savePanel
method.
© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-11-07)