我注意到了几件事..更改setCanChooseDirectories
为否。启用后,这表示文件夹是有效输入。这很可能不是您想要的功能。您可能还希望将允许的文件类型更改[NSArray arrayWithObject:@"pdf", @"PDF", nil]
为区分大小写的系统。runModalForTypes
应该是文件类型的数组。将您的代码更改为如下所示:
// WORKING :)
NSOpenPanel *panel;
NSArray* fileTypes = [NSArray arrayWithObjects:@"pdf", @"PDF", nil];
panel = [NSOpenPanel openPanel];
[panel setFloatingPanel:YES];
[panel setCanChooseDirectories:NO];
[panel setCanChooseFiles:YES];
[panel setAllowsMultipleSelection:YES];
[panel setAllowedFileTypes:fileTypes];
int i = [panel runModal];
if(i == NSOKButton){
return [panel URLs];
}
斯威夫特 4.2:
let fileTypes = ["jpg", "png", "jpeg"]
let panel = NSOpenPanel()
panel.canChooseFiles = true
panel.canChooseDirectories = false
panel.allowsMultipleSelection = false
panel.allowedFileTypes = fileTypes
panel.beginSheetModal(for: window) { (result) in
if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
// Do something with the result.
let selectedFolder = panel.urls[0]
print(selectedFolder)
}
}