12

我想知道如何使 Interface Builder 中的按钮或输入字段以这样的方式做出反应,即单击它会打开一个文件对话框并让您选择一个或多个文件并将它们放入指定的数组/表中......

一旦按下按钮并选择文件(这似乎是一件非常微不足道的事情),我想它已经包含某种数组(比如带有所选文件路径的数组)所以我已经涵盖了......我只需要知道如何将按钮链接到文件选择器以及文件选择器以何种方式将文件传递给我(或文件的路径),以便我可以将它们重定向到数组

有没有一种简单的方法可以做到这一点,更重要的是;是否有文件选择器或者我必须使用 XCode 而不是 Interface builder 来执行此操作?

4

3 回答 3

10

这必须在 Xcode 中完成。这里的代码应该可以正常工作。

只需将按钮与使用 IB 的方法挂钩,并使用该示例作为在方法中放入内容的指南。

Cocoadev 的 WRT NSOpenPanel也有各种很好的帮助,包括将面板作为工作表而不是模式窗口打开的提示。

当然,您也应该始终阅读Apple 文档

于 2009-01-26T23:33:54.137 回答
8

我在查找时发现了这个页面,如何在 Cocoa 中打开文件打开框。随着 OS X 10.7 的发布,许多链接到的示例现已弃用。因此,这里有一些示例代码可以为您节省一些编译器警告:

// -----------------
// NSOpenPanel: Displaying a File Open Dialog in OS X 10.7
// -----------------

// Any ole method
- (void)someMethod {
  // Create a File Open Dialog class.
  NSOpenPanel *openDlg = [NSOpenPanel openPanel];

  // Set array of file types 
  NSArray<NSString*> *fileTypesArray = @[@"jpg", @"gif", @"png"];

  // Enable options in the dialog.
  [openDlg setCanChooseFiles:YES];    
  [openDlg setAllowedFileTypes:fileTypesArray];
  [openDlg setAllowsMultipleSelection:YES];

  // Display the dialog box.  If OK is pressed, process the files.
  if ([openDlg runModal] == NSModalResponseOK) {
    // Get list of all files selected
    NSArray<NSURL*> *files = [openDlg URLs];

    // Loop through the files and process them.
    for (NSURL *file in files) {
      // Do something with the filename.
      NSLog(@"File path: %@", [file path]);
    }
  }
}
于 2012-02-21T09:41:55.947 回答
5

Interface Builder 用于设计和链接界面。你想打开文件并将它们放在一个数组中,这在 Xcode 方面是安全的。让按钮的操作显示 NSOpenPanel 并将结果提供给表的数据源。

于 2009-01-26T23:17:36.367 回答