if I want to show a window as a sheet in macOS, does that NSWindow have to be part of the main window’s xib file, or can it be a separate NSWindow/xib file in the solution?
I recommend a separate xib. I can post a sample later today.
Ok, so basically, just
- create a new Window+XIB from template
- make sure (IIRC) that "Visible at Launch: is unchecked in the XIB, else your window may fail to load
- instatiame your window controller as normal
- in your sheet view controller, add a “shiwForWindow” message that calls
NSApp.beginSheet(this.window) modalForWindow(parentWindow) modalDelegate(this) didEndSelector(__selector(didEndSheet:returnCode:contextInfo:)) contextInfo(null);
- also in the wc, implement didEndSheet(NSWindow) returnCode(int) contextInfo(void*)`.
eg (sorry for the C#, im grabbing this from Fire):
[IBObject]
public partial class SBLBaseSheetController: NSWindowController
{
private void showForWindow(Window aWindow)
{
parentWindow = aWindow;
retainedSelf = this; // keep a string reference around, unless your caller does...
NSApp.beginSheet(this.window) modalForWindow(aWindow) modalDelegate(this) didEndSelector(__selector(didEndSheet:returnCode:contextInfo:)) contextInfo(null);
}
private void didEndSheet(NSWindow sheet) returnCode(int returnCode) contextInfo(void* contextInfo)
{
window.orderOut(this.window());
retainedSelf = null;
}
private __strong SBLBaseSheetController retainedSelf;
}
hth.
thanks marc - huge help. looking at it now.
Hey marc -
I’m assuming showForWindow is a ‘non-standard’ method that I need to make - not part of INSWIndowController? I couldn’t find it in the interface. I’m also assuming I need to do essentially this:
var MySheetWindow := new SheetWindow1(self);
MySheetWindow.showForWindow(self.Window);
Is this correct? Did I understand your directions correctly?
I tried implementing this while watching some TV, but kept getting an exception when trying ot show my sheet. I’ll look more first thing in the morning, but please correct me if I’m making a dumbass tired mistake here tonight.
Thanks again,
Alan
correct, yes.
ok, finally getting something to work…but the window doesn’t appear as a sheet. it appears as normal dialog. I did set Visible At launch to false, by the way.
Here is some of my code.
In initial window that calls the sheet:
var NewJob := new AddJobWindow;
NewJob.showForWindow(self.window);
in the 'sheet window
method AddJobWindow.showForWindow(aWindow: NSWindow);
begin
NSLog('In showForWindow');
try
parentwindow := aWindow;
NSApp.beginSheet(self.window) modalForWindow(parentwindow) modalDelegate(self) didEndSelector( selector(didEndSheet:returnCode:contextInfo:)) contextInfo(nil);
except
on ex:Exception do
begin
NSLog(ex.Message);
NSLog(ex.StackTrace);
end;
end;
end;
Hmm. Send me the project, I’ll have a look.
I figured it out. I didn’t have my window outlets pointing to first responder…so self.window was nil.
What do you use to close the modal sheet? Do you call NSApp.stopModal? or something else?
NSApp.endSheet(window);
what if I need an OK button and Cancel button, where do I set the return code that gets passed to sheetDidEnd?
nevermind, I found what I needed I believe.