swift – NSOpenPanel作为表格

前端之家收集整理的这篇文章主要介绍了swift – NSOpenPanel作为表格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我看了看其他答案,但似乎没有任何帮助我的情况.

我有一个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")
    }
})
原文链接:https://www.f2er.com/swift/319163.html

猜你在找的Swift相关文章