我有一个nsopenpanel。但我想让它的pdf文件只能选择。我在找这样的东西:

// NOT WORKING
NSOpenPanel *panel;

panel = [NSOpenPanel openPanel];
[panel setFloatingPanel:YES];
[panel setCanChooseDirectories:YES];
[panel setCanChooseFiles:YES];
[panel setAllowsMultipleSelection:YES];
[panel setAllowedFileTypes:[NSArray arrayWithObject:@"pdf"]];
int i = [panel runModalForTypes:nil];
if(i == NSOKButton){
    return [panel filenames];
}

我希望有人能找到解决办法。

最佳答案

有几件事我注意到了…将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)
    }
}

关于objective-c - NSOpenPanel setAllowedFileTypes,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4294168/

10-13 08:08