Nininea
Nininea

Reputation: 2739

BeginSheet doesn't show window

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

Answers (2)

pkamb
pkamb

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:

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

}

Upvotes: 4

Kaixin Lian
Kaixin Lian

Reputation: 224

Try this:

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

Upvotes: 3

Related Questions