- Inherits from:
- NSSavePanel : NSPanel : NSWindow : NSResponder : NSObject
- Conforms to:
- NSObject
- (NSObject)
Declared in:
- AppKit/NSOpenPanel.h
NSOpenPanel provides the Open panel for the Cocoa user interface. Applications use the Open panel as a convenient way to query the user for the name of a file to open. The Open panel can only be run modally.
Most of this class's behavior is defined by its superclass, NSSavePanel. NSOpenPanel adds to this behavior by:
Typically, you access an NSOpenPanel by invoking the openPanel class method. When the class receives an openPanel message, it tries to reuse an existing panel rather than create a new one. If a panel is reused, its attributes are reset to the default values so that the effect is the same as receiving a new panel. Because Open panels may be reused, you shouldn't modify the instance returned by openPanel except through the methods listed below (and those inherited from NSSavePanel). For example, you can set the panel's title and whether it allows multiple selection, but not the arrangement of the buttons within the panel. If you must modify the Open panel substantially, create and manage your own instance using the alloc... and init... methods rather than the openPanel method.
The following code example shows the NSOpenPanel displaying
only files with extensions of ".td" and allowing multiple
selection. If the user makes a selection and clicks the OK button
(that is, runModalForDirectory:file:types: returns NSOKButton
),
this method opens each selected file:
- (void)openDoc:(id)sender { int result; NSArray *fileTypes = [NSArray arrayWithObject:@"td"]; NSOpenPanel *oPanel = [NSOpenPanel openPanel]; [oPanel setAllowsMultipleSelection:YES]; result = [oPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes]; if (result == NSOKButton) { NSArray *filesToOpen = [oPanel filenames]; int i, count = [filesToOpen count]; for (i=0; i<count; i++) { NSString *aFile = [filesToOpen objectAtIndex:i]; id currentDoc = [[ToDoDoc alloc] initWithFile:aFile]; } } }
- Obtaining the shared instance
- + openPanel
- Running the panel modally
- - runModalForDirectory:file:types:
- - runModalForTypes:
- Getting the user selection
- - filenames
- Allowing browser selections
- - setCanChooseFiles:
- - canChooseFiles
- - setCanChooseDirectories:
- - canChooseDirectories
- Allowing multiple selections
- - setAllowsMultipleSelection:
- - allowsMultipleSelection
+ (NSOpenPanel *)openPanel
- (BOOL)allowsMultipleSelection
nil
value only if one and only
one file is selected. By contrast, NSOpenPanel's filenames method
always returns the selected files, even if only one file is selected. See Also: - filename (NSSavePanel), - filenames, - setAllowsMultipleSelection:
- (BOOL)canChooseDirectories
See Also: - setCanChooseDirectories:
- (BOOL)canChooseFiles
See Also: - setCanChooseFiles:
- (NSArray *)filenames
- (int)runModalForDirectory:(NSString
*)directory
file:(NSString *)filename
types:(NSArray *)fileTypes
NSOKButton
or NSCancelButton
,
respectively. The NSOpenPanel displays the files
in directory (an absolute directory
path) that match the types in fileTypes (an
NSArray of file extensions). If directory is nil
the
default directory is the application directory. If all files in
a directory should appear in the browser, fileTypes should
be nil
. You can control whether directories
and files appear in the browser with the setCanChooseDirectories: and setCanChooseFiles: methods.
The filename argument specifies a
particular file in directory that
is selected when the Open panel is presented to the user; otherwise, filename should
be nil
. See Also: - runModalForTypes:
- (int)runModalForTypes:(NSArray
*)fileTypes
nil
for both the filename and directory arguments. See
the description of runModalForDirectory:file:types: for
details. The fileTypes argument is
an NSArray containing the extensions of files to be shown in the
browser. Returns NSOKButton
(if the
user clicks the OK button) or NSCancelButton
(if
the user clicks the Cancel button).- (void)setAllowsMultipleSelection:(BOOL)flag
See Also: - allowsMultipleSelection
- (void)setCanChooseDirectories:(BOOL)flag
YES
.See Also: - canChooseDirectories
- (void)setCanChooseFiles:(BOOL)flag
See Also: - canChooseFiles
- (NSArray *)URLs