NSOpenPanel as a sheet

Ive looked around for other answers, but nothing seems to help my case.

I have a viewController class that contains an IBAction button for a button. This button should open the NSOpenPanel as a sheet from this viewController:

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") } } } 

The problem arises from myFileDialog.beginSheetModalForWindow (..), it works with the line above, but this is not a sheet effect

+6
source share
1 answer

You need to call beginSheetModalForWindow from the panel in your window and use the completion block:

 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") } }) 
+11
source

Source: https://habr.com/ru/post/985032/


All Articles