Reputation: 2739
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.
Upvotes: 3
Views: 1617
Reputation: 35032
I just made this same error, and had trouble with it for quite some time. I was following along with the Apple guide:
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
}
Upvotes: 4
Reputation: 224
Try this:
let panel = NSOpenPanel()
self.window?.beginSheet(panel, completionHandler: { (modalResponse: NSModalResponse) in
if modalResponse == NSModalResponseOK {
// do your stuff
}
})
Upvotes: 3