Using a Save Panel

Typically, you access an NSSavePanel by invoking the savePanel class 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 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.