macos 显示文件选择器对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5126594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:45:38  来源:igfitidea点击:

Display file chooser dialog

objective-cmacoscocoa

提问by Johnny Mast

How do I show a file chooser dialog on Mac OS X? The language is Objective C.

如何在 Mac OS X 上显示文件选择器对话框?语言是Objective C。

回答by evotopid

What you search is 'NSOpenPanel', here a example how to use:

您搜索的是“NSOpenPanel”,这里是一个如何使用的示例:

NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:NO];
[panel setCanChooseDirectories:YES];
[panel setAllowsMultipleSelection:YES]; // yes if more than one dir is allowed

NSInteger clicked = [panel runModal];

if (clicked == NSFileHandlingPanelOKButton) {
    for (NSURL *url in [panel URLs]) {
        // do something with the url here.
    }
}

回答by Anand

Those who are looking for Swift version

正在寻找Swift 版本的人

let panel                     = NSOpenPanel()
panel.canChooseDirectories    = false
panel.canChooseFiles          = true
panel.allowsMultipleSelection = false
panel.allowedFileTypes        = ["txt"]
let clicked                   = panel.runModal()

if clicked == NSApplication.ModalResponse.OK {
    print("URLS => \(panel.urls)")
}