打开文件对话框

时间:2009-10-28 22:02:28

标签: objective-c user-interface dialog openfiledialog gnustep

我正在学习Objective-C并尝试开发一个简单的拉链应用程序,但我现在停止了,当我需要在对话框中插入一个按钮时,此按钮打开一个打开文件对话框,它将选择要压缩的文件,但我从未使用过“打开文件”对话框,那么我如何打开它并将用户选择的文件存储在char*中?感谢。

请记住,我正在使用GNUstep(Linux)。

3 个答案:

答案 0 :(得分:20)

感谢@Vlad Impala我正在为使用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);
    }
}

答案 1 :(得分:16)

如果其他人需要这个答案,这里是:

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

答案 2 :(得分:2)

对于使用OS X v10.10 +的用户,请在Far Jangtrakool中替换答案:

if ( [openDlg runModal] == NSOKButton )

通过

if ( [openDlg runModal] == NSModalResponseOK )