Pregunta

Estoy aprendiendo Objective-C e intento desarrollar una aplicación de cremallera simple, pero me detuve cuando ahora, cuando necesito 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.

Recuerda que estoy usando GNUstep (Linux).

¿Fue útil?

Solución

En caso de que alguien más necesite 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];
   }

Otros consejos

Gracias @Vlad the Impala Estoy actualizando sus respuestas para las personas que usan 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);
    }
}

Para las personas que usan OS X v10.10 +, reemplace en Far Jangtrakool respuesta:

if ( [openDlg runModal] == NSOKButton )

por

if ( [openDlg runModal] == NSModalResponseOK )
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top