显示文件选择器对话框

时间:2011-02-26 11:24:12

标签: objective-c macos cocoa

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

2 个答案:

答案 0 :(得分:30)

你搜索的是'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.
    }
}

答案 1 :(得分:2)

正在寻找 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)")
}