使用 NSOpenPanel 限制对某些文件夹的访问
我使用 NSOpenPanel 允许用户选择一个文件夹来保存文档。我想限制它们可以保存到哪个文件夹(就层次结构而言)。本质上,我想阻止他们选择上面的任何文件夹:
/用户/用户名/
所以文件夹
/用户/用户名/猫/
是可以接受的,但是
/用户/用户名/
/应用程序/猫/
不允许 。我想知道如何实施这个限制。
谢谢。
I'm using NSOpenPanel to allow a user to select a folder to save documents into. I would like to restrict what folder (in terms of hierarchy) they can save into. Essentially, I want to prevent them from choosing any folder above:
/Users/username/
So the folder
/Users/username/cats/
would be acceptable but
/Users/username/
/Applications/cats/
would not be allowed. I was wondering how to implement this restriction.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意
NSOpenPanel
继承自NSSavePanel
,它又定义了委托和相应的委托协议NSOpenSavePanelDelegate
。您可以使用委托来扩展打开面板的行为,以便包含您在问题中列出的限制。例如,假设应用程序委托实现了打开面板限制,请使其符合
NSOpenSavePanelDelegate
协议:在应用程序委托的实现中,告诉打开面板应用程序委托充当打开面板委托:
并实现以下委托方法:
Note that
NSOpenPanel
inherits fromNSSavePanel
, which in turn defines a delegate and a corresponding delegate protocolNSOpenSavePanelDelegate
. You can use the delegate to extend the behaviour of the open panel so as to include the restriction you’ve listed in your question.For instance, assuming the application delegate implements the open panel restriction, make it conform to the
NSOpenSavePanelDelegate
protocol:In the implementation of your application delegate, tell the open panel that the application delegate acts as the open panel delegate:
And implement the following delegate methods:
因此,我尝试将其更新为 Swift 5.5。
我将所有委托方法都包含在内,以便让任何偶然发现此问题的人清楚地了解。
在我的 ViewController 中,我有一个“Utility”实例作为“utility”,还有一个 imageButton 的 IBAction,其 Storyboard 标识为“fileBrowseImageButton”。
用法:
So, I took a stab at updating this for Swift 5.5.
I am including all the delegate methods for clarity to anyone who stumbles across this.
In my ViewController I have an instance of “Utility“ as “utility“ and an IBAction for an imageButton that has a Storyboard identity of “fileBrowseImageButton“.
Usage: