Pergunta

Estou aprendendo Objective-C e tentando desenvolver uma aplicação simples zipper, mas parei quando agora, quando eu preciso inserir um botão no meu diálogo e este botão abre uma caixa de diálogo Abrir arquivo que irá selecionar um arquivo para comprimir , mas eu nunca usei um diálogo abrir arquivo, então como eu posso abri-lo e armazenar o arquivo selecionado usuário em um char*? Obrigado.

Lembre-se que eu estou usando GNUstep (Linux).

Foi útil?

Solução

Em caso de alguém precisa dessa resposta, aqui 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];
   }

Outras dicas

Graças @Vlad o Impala Estou atualizando suas respostas para pessoas que usam 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 as pessoas que usam OS X v10.10 +, substitua em resposta Far Jangtrakool:

if ( [openDlg runModal] == NSOKButton )

por

if ( [openDlg runModal] == NSModalResponseOK )
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top