3

I have simple code :

var openPanel = NSOpenPanel();
openPanel.beginSheet(this.View.Window, (obj) => {
    //do stuff
    openPanel.endSheet(this.View.Window);
});

Sometime the sheet window is not shown and makes a sound like the window is busy. Is there anything wrong in my code?

I call this code from one item of splitViewcontroller.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Nininea
  • 2,671
  • 6
  • 31
  • 57

2 Answers2

4

I just made this same error, and had trouble with it for quite some time. I was following along with the Apple guide:

Using the Open and Save Panels

The main issue is that the Apple docs show us using the Objective-C method:

[panel beginSheetModalForWindow:window completionHandler:^(NSInteger result){    }

I did an ad hoc translation into Swift, with help from Xcode autocomplete:

let openPanel = NSOpenPanel()
openPanel.beginSheet(window) { (modalResponse: NSApplication.ModalResponse) in
}

This does not work. When the code is run, the Window's title bar disappears + no panel is shown.

Use the correct Swift method instead, beginSheetModal:

openPanel.beginSheetModal(for: window) { (modalResponse: NSApplication.ModalResponse) in

}
Community
  • 1
  • 1
pkamb
  • 33,281
  • 23
  • 160
  • 191
3

Try this:

let panel = NSOpenPanel()
self.window?.beginSheet(panel, completionHandler: { (modalResponse: NSModalResponse) in
    if modalResponse == NSModalResponseOK {
        // do your stuff
    }
})
Kaixin Lian
  • 204
  • 2
  • 5