我看了看其他答案,但似乎没有任何帮助我的情况.
我有一个viewController类,它包含一个按钮的IBAction.此按钮应该从该viewController打开NSOpenPanel作为工作表:
class ViewController: NSViewController { @IBAction func folderSelection(sender: AnyObject) { var myFiledialog: NSOpenPanel = NSOpenPanel() myFiledialog.prompt = "Select path" myFiledialog.worksWhenModal = true myFiledialog.allowsMultipleSelection = false myFiledialog.canChooseDirectories = true myFiledialog.canChooseFiles = false myFiledialog.resolvesAliases = true //myFiledialog.runModal() myFiledialog.beginSheetModalForWindow(self.view.window!,completionHandler: nil) var chosenpath = myFiledialog.URL if (chosenpath!= nil) { var TheFile = chosenpath!.absoluteString! println(TheFile) //do something with TheFile } else { println("nothing chosen") } } }
问题来自myFileDialog.beginSheetModalForWindow(..),它适用于上面的行,但这不是一个工作表效果
您需要从窗口的面板调用beginSheetModalForWindow,并使用完成块:
let myFiledialog = NSOpenPanel() myFiledialog.prompt = "Select path" myFiledialog.worksWhenModal = true myFiledialog.allowsMultipleSelection = false myFiledialog.canChooseDirectories = true myFiledialog.canChooseFiles = false myFiledialog.resolvesAliases = true myFiledialog.beginSheetModalForWindow(window,completionHandler: { num in if num == NSModalResponSEOK { let path = myFiledialog.URL print(path) } else { print("nothing chosen") } })