显示文件选择器对话框

Joh*_*ast 14 macos cocoa objective-c

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

evo*_*pid 31

你搜索的是'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.
    }
}
Run Code Online (Sandbox Code Playgroud)


Ana*_*and 5

正在寻找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)")
}
Run Code Online (Sandbox Code Playgroud)