NSOpenPanel setAllowedFileTypes

发布于 2024-10-04 23:05:23 字数 461 浏览 5 评论 0原文

我有一个 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];
}

我希望有人能找到解决方案。

I have a NSOpenPanel. But I want to make it PDF-files selectable only. I'm looking for something like that:

// 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];
}

I hope someboby has a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捶死心动 2024-10-11 23:05:23

我注意到了一些事情......将 setCanChooseDirectories 更改为 NO。启用后,这表明文件夹是有效输入。这很可能不是您想要的功能。对于区分大小写的系统,您可能还需要将允许的文件类型更改为 [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];
}

Swift 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)
    }
}

A couple things I noticed.. change setCanChooseDirectoriesto NO. When enabled this indicates that folders are valid input. This is most likely not the functionality you want. You might also want to change your allowed file types to [NSArray arrayWithObject:@"pdf", @"PDF", nil] for case sensitive systems. runModalForTypes should be the array of file types. Change your code to look like this:

// 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];
}

Swift 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)
    }
}
素手挽清风 2024-10-11 23:05:23

你已经非常接近答案了。

首先,删除[panel setCanChooseDirectories:YES],这样它就不会允许目录。

然后,将 [panel runModalForTypes:nil] 更改为 [panel runModal] 或删除对 [panel setAllowedFileTypes:] 的调用并传递数组改为 [panel runModalForTypes:]

You are very close to the answer.

First, get rid of [panel setCanChooseDirectories:YES] so that it won't allow directories as a result.

Then, either change[panel runModalForTypes:nil] to [panel runModal] or get rid of the call to [panel setAllowedFileTypes:] and pass the array to [panel runModalForTypes:] instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文