2009-10-28 23 views
7

Estoy aprendiendo Objective-C y tratando de desarrollar una aplicación de cremallera simple, pero me detuve cuando ahora, cuando tengo que insertar un botón en mi cuadro de diálogo y este botón abre un cuadro de diálogo Abrir archivo que seleccionará un archivo para comprimir, pero nunca utilicé un cuadro de diálogo Abrir archivo, ¿cómo puedo abrirlo y almacenar el archivo seleccionado por el usuario en un char*? Gracias.Abrir cuadro de diálogo de archivo

Recuerde que estoy usando GNUstep (Linux).

Respuesta

16

En caso de que alguien más tiene esta respuesta, aquí está:

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

Gracias @Vlad el Impala Estoy actualizando sus respuestas para las personas que utilizan 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

para las personas que utilizan OS X v10.10 +, vuelva a colocar en respuesta Lejos Jangtrakool:

if ([openDlg runModal] == NSOKButton) 

por

if ([openDlg runModal] == NSModalResponseOK) 
Cuestiones relacionadas