相当于NSOpenPanel的swift 2.0等价物

时间:2015-09-14 17:54:45

标签: cocoa swift2 nsopenpanel

我找到了这个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下创建Cocoa类而没有任何运气。

2 个答案:

答案 0 :(得分:1)

如果还没有,请尝试导入AppKit:

import AppKit

您可以阅读上面的Apple Docs

答案 1 :(得分:0)

作为奖励,

带有弹出窗口的自定义视图。

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
}

}