macos 如何使用 Interface Builder 在可可中添加文件选择器/开启器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/481780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a file selector/opener in cocoa with Interface Builder?
提问by xaddict
I'm wondering how to make a button or input field in Interface Builder react in such a way that on click it opens a file dialog and lets you select one or more files and puts them into a specified array/table...
我想知道如何使 Interface Builder 中的按钮或输入字段以这样一种方式做出反应,即单击它会打开一个文件对话框,让您选择一个或多个文件并将它们放入指定的数组/表中...
Once the button is pushed and files are chosen (this seems a like quite trivial thing) I guess it will already contain some sort of array (like an array with paths to the selected files) so I've got that covered.. I only need to know how to link the button to a file selector and in what way the file selector delivers the files to me (or paths to the files) so I can redirect them to the array
一旦按下按钮并选择文件(这似乎是一件非常微不足道的事情)我想它已经包含某种数组(例如带有所选文件路径的数组)所以我已经涵盖了......我只需要知道如何将按钮链接到文件选择器以及文件选择器以何种方式将文件传送给我(或文件的路径),以便我可以将它们重定向到数组
Is there an easy way to do this, and more importantly; is there a file selector thingie or do I have to do this with XCode instead of Interface builder?
有没有一种简单的方法可以做到这一点,更重要的是;是否有文件选择器,或者我必须使用 XCode 而不是 Interface builder 来执行此操作?
采纳答案by Rich Catalano
This must be done in Xcode. The code hereshould work fine.
这必须在 Xcode 中完成。这里的代码应该可以正常工作。
Just hook the button up with a method using IB and use that example as a guide of what to put in the method.
只需将按钮与使用 IB 的方法连接起来,并使用该示例作为在方法中放置内容的指南。
There's also all sorts of good help WRT NSOpenPanel at Cocoadev, including tips on opening the panel as a sheet instead of a modal window.
在Cocoadev上还有各种很好的帮助 WRT NSOpenPanel ,包括将面板作为工作表而不是模式窗口打开的提示。
Of course you should always read the Apple documentationas well.
当然,您也应该始终阅读Apple 文档。
回答by archieoi
I found this page when looking up, how to open up a file open box in Cocoa. With the release of OS X 10.7 a lot of the samples that is linked to is now deprecated. So, here is some sample codethat will save you some compiler warnings:
我在查找时发现了这个页面,如何在 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]);
}
}
}
回答by Chuck
Interface Builder is for designing and linking together the interface. You want to open files and put them in an array, which is safely on the Xcode side of things. Have the button's action show an NSOpenPanel and give the results to your table's data source.
Interface Builder 用于设计和链接界面。您想打开文件并将它们放入一个数组中,这在 Xcode 方面是安全的。让按钮的动作显示一个 NSOpenPanel 并将结果提供给表的数据源。