2009-10-28 23 views
7

Uczę się Objective-C i próbuję stworzyć prostą aplikację na zamek błyskawiczny, ale zatrzymałem się, kiedy teraz, kiedy muszę wstawić przycisk w moim oknie dialogowym, a ten przycisk otwiera okno dialogowe Otwórz plik, które wybierze plik do kompresji, ale nigdy nie korzystałem z okna dialogowego Otwórz plik, a następnie w jaki sposób mogę je otworzyć i zapisać wybrany plik użytkownika w numerze char*? Dzięki.Okno dialogowe otwierania pliku

Pamiętaj, że używam GNUstep (Linux).

Odpowiedz

16

W przypadku gdy ktoś inny musi tę odpowiedź, to jest tutaj:

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

Dzięki @Vlad Impala ja aktualizując odpowiedzi dla ludzi, którzy korzystają z OS X 10.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

dla ludzi, którzy korzystają z OS X v10.10 + zastąpić w Far Jangtrakool odpowiedź:

if ([openDlg runModal] == NSOKButton) 

przez

if ([openDlg runModal] == NSModalResponseOK) 
Powiązane problemy