xad*_*ict 10 macos cocoa interface-builder
我想知道如何使Interface Builder中的按钮或输入字段以这样的方式作出反应:点击它打开一个文件对话框,让你选择一个或多个文件并将它们放入指定的数组/表...
一旦按下按钮并选择了文件(这看起来像是一件非常简单的事情)我想它已经包含某种类型的数组(就像一个包含所选文件路径的数组)所以我已经覆盖了..我只有需要知道如何将按钮链接到文件选择器以及文件选择器以何种方式将文件传递给我(或文件的路径),以便我可以将它们重定向到数组
有没有一种简单的方法可以做到这一点,更重要的是; 是否有文件选择器thingie或我是否必须使用XCode而不是Interface builder?
我在查找时找到了这个页面,如何在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]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Interface Builder用于设计和链接接口.你想打开文件并将它们放在一个数组中,这是安全地在Xcode方面.让按钮的动作显示NSOpenPanel并将结果提供给表的数据源.
| 归档时间: |
|
| 查看次数: |
20007 次 |
| 最近记录: |