2009-10-28 4 views
7

Я изучаю Objective-C и пытаюсь разработать простое приложение для zipper, но теперь я остановился, когда мне нужно вставить кнопку в мой диалог, и эта кнопка открывает диалоговое окно «Открыть файл», в котором будет выбран файл для сжатия, но я никогда не использовал Open File Dialog, тогда как я могу открыть его и сохранить выбранный пользователем файл в char*? Благодарю.Open File Dialog Box

Помните, что я использую GNUstep (Linux).

ответ

16

В случае, если кто-то еще нужен этот ответ, вот оно:

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

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

    // Multiple files not allowed 
    [openDlg setAllowsMultipleSelection:NO]; 

    // Can't select a directory 
    [openDlg setCanChooseDirectories:NO]; 

    // 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]; 
    } 
18

Благодаря @Vlad Импале Я обновляя свои ответы на людей, которые используют OS X v10.6 +

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

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

// Multiple files not allowed 
[openDlg setAllowsMultipleSelection:NO]; 

// Can't select a directory 
[openDlg setCanChooseDirectories:NO]; 

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

    // Loop through all the files and process them. 
    for(int i = 0; i < [urls count]; i++) 
    { 
     NSString* url = [urls objectAtIndex:i]; 
     NSLog(@"Url: %@", url); 
    } 
} 
2

Для людей, которые используют OS X v10.10 + заменить в Far Jangtrakool ответ:

if ([openDlg runModal] == NSOKButton) 

по

if ([openDlg runModal] == NSModalResponseOK)