Question

J'apprends à utiliser Objective-C et j'essaye de développer une application de fermeture à glissière simple, mais je me suis arrêté quand, quand j'ai besoin d'insérer un bouton dans ma boîte de dialogue, ce bouton ouvre une boîte de dialogue Ouvrir un fichier qui sélectionne un fichier à compresser. , mais je n'ai jamais utilisé de boîte de dialogue Ouvrir un fichier, comment puis-je l'ouvrir et stocker le fichier sélectionné par l'utilisateur dans un char * ? Merci.

N'oubliez pas que j'utilise GNUstep (Linux).

Était-ce utile?

La solution

Si quelqu'un a besoin de cette réponse, la voici:

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

Autres conseils

Merci @Vlad the Impala Je mets à jour vos réponses pour les personnes qui utilisent 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);
    }
}

Si vous utilisez OS X v10.10 +, remplacez dans Far Jangtrakool, répondez:

if ( [openDlg runModal] == NSOKButton )

par

if ( [openDlg runModal] == NSModalResponseOK )
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top