简体   繁体   English

打开文件对话框

[英]Open File Dialog Box

I'm learning Objective-C and trying to develop a simple zipper application, but I stopped when now, when I need to insert a button at my dialog and this button opens a Open File Dialog that will select a file to compress, but I never used a Open File Dialog, then how I can open it and store the user selected file in a char* ? 我正在学习Objective-C并试图开发一个简单的拉链应用程序,但是我现在停止了,当我需要在对话框中插入一个按钮时,这个按钮打开一个打开文件对话框,它将选择要压缩的文件,但是从未使用过打开文件对话框,然后我如何打开它并将用户选择的文件存储在char* Thanks. 谢谢。

Remember that I'm using GNUstep(Linux). 请记住,我正在使用GNUstep(Linux)。

Thanks @Vlad the Impala I am updating your answers for people who use OS X v10.6+ 谢谢@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);
    }
}

In case someone else needs this answer, here it is: 如果其他人需要这个答案,这里是:

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

For people who use OS X v10.10+, replace in Far Jangtrakool answer: 对于使用OS X v10.10 +的用户,请在Far Jangtrakool中替换答案:

if ( [openDlg runModal] == NSOKButton )

by 通过

if ( [openDlg runModal] == NSModalResponseOK )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM