< Previous PageNext Page > Hide TOC

Using a Save Panel

Typically, you access an NSSavePanel by invoking the savePanelclass method. A typical programmatic use of NSSavePanel requires you to:

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.



< Previous PageNext Page > Hide TOC


© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-11-07)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.