File Open Dialog

on May 13th, 2012 by Hades | No Comments »

Отображение окна выбора файла в Cocoa можно выполнить следующим образом
[cc lang=”objc” width=”550″]
– (IBAction)ShowFileDialog:(id)sender {
int i; // Loop counter.

// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];

// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];

// Enable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:YES];

// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* files = [openDlg filenames];

// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ ) { NSString* fileName = [files objectAtIndex:i]; // Do something with the filename. NSAlert *alert = [[[NSAlert alloc] init] autorelease]; [alert setMessageText:fileName]; [alert runModal]; } } } [/cc]