swift2 用于NSOpenPanel的swift 2.0等效程序

4nkexdtk  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(263)

我找到了这个swift 1.2教程来打开一个面板。但是它在swift 2.0中不工作。

@IBAction func selectFile(sender: AnyObject) {
    var openPanel = NSOpenPanel()
    openPanel.title = "Select file"
    openPanel.beginWithCompletionHandler({(result:Int) in
        if (result = NSFILEHandlingPanelOKButton){
            print(openPanel.URL!)
        }
    })
}

我得到了错误的未解析标识符NSOpenPanel,swift 2.0的等效项是什么?
我也尝试过在iOS和MacOS下创建可可类,但没有任何运气。

mgdq6dx1

mgdq6dx11#

如果尚未导入,请尝试导入AppKit:

import AppKit

你可以在上面看到Apple Docs

mwg9r5ms

mwg9r5ms2#

作为奖励,一个自定义视图与弹出。

func chooseDestFolder()->URL?{

//it's an OPEN... :)
let dialog = NSOpenPanel()

//dialog.title                   = "Choose destination folder"
dialog.message                 = "Choose destination folder"
dialog.showsResizeIndicator    = true
dialog.showsHiddenFiles        = false
dialog.canChooseDirectories    = true
dialog.canChooseFiles          = false
dialog.canCreateDirectories    = true
dialog.allowsMultipleSelection = false
dialog.allowedFileTypes        = [];

let sv = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 40))

let menu = NSPopUpButton(radioButtonWithTitle: "AAA", target: nil, action: nil)
menu.frame = CGRect(x: 0, y: 10, width: 100, height: 36)
menu.addItems(withTitles: ["JPG", "PDF", ])
sv.addSubview(menu)

dialog.accessoryView = sv
dialog.accessoryView?.wantsLayer = true
//dialog.accessoryView?.layer?.backgroundColor = NSColor.red.cgColor
dialog.isAccessoryViewDisclosed = true
if (dialog.runModal() == NSApplication.ModalResponse.OK) {
    let destUrl = dialog.url
    return destUrl
} else {
    // User clicked on "Cancel"
    return nil
}

}

相关问题