我在查找时发现了这个页面,如何在 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]);
}
}
}