NSOpenPanel setAllowedFileTypes
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我注意到了一些事情......将 setCanChooseDirectories 更改为 NO。启用后,这表明文件夹是有效输入。这很可能不是您想要的功能。对于区分大小写的系统,您可能还需要将允许的文件类型更改为
[NSArray arrayWithObject:@"pdf", @"PDF", nil]
。runModalForTypes
应该是文件类型的数组。将代码更改为如下所示:Swift 4.2:
A couple things I noticed.. change
setCanChooseDirectories
to 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:Swift 4.2:
你已经非常接近答案了。
首先,删除
[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.