alex
alex

Reputation: 4924

swift 2.0 equivalent for NSOpenPanel

i found this swift 1.2 tutorial to open up a panel. but it doesn't work in 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!)
        }
    })
}

I am getting the error unresolved identifier NSOpenPanel, what would be the swift 2.0 equivalent?

I also tried creating Cocoa class under iOS and MacOS without any luck.

Upvotes: 0

Views: 896

Answers (2)

ingconti
ingconti

Reputation: 11656

as a bonus a custom view with popover..

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
}

}

Upvotes: 0

Caleb Kleveter
Caleb Kleveter

Reputation: 11494

If you haven't, try importing AppKit:

import AppKit

You can read the Apple Docs on it.

Upvotes: 2

Related Questions