Search code examples
dialogmodal-dialognswindow

NSheet controller doesn't get run when the sheet is displayed.


I have, in the Objective-C environment, a custom controller "MyWindowController" as File's Owner of the related XIB, to be used as a modal sheet over my main window.

Invoking as follows per Apple documentation (minus deprecations):

//in the main's awakeFromNib
MyWindowController = [[NSWindowController alloc]
                    initWithWindowNibName:@"MyWindowController"];

//in the main place where needed
[NSApp beginSheet: MyWindowController.window
        modalForWindow: _window
        modalDelegate: nil
        didEndSelector: nil
        contextInfo: nil ];
[NSApp runModalForWindow: MyWindowController];
// Dialog is up here.
[NSApp endSheet: MyWindowController.window];
[MyWindowController.window orderOut:self];

For now, MyWindowController is a minimal subclass of NSWindowController, including the following for a button:

- (IBAction)doCancel:(id)sender
{
//...set return value
    [NSApp stopModal];
}

The sheet appears properly when invoked by the running main code, but the code in the controller never runs. Breakpoints set in MyWindowController, including one at its awakeFromNib are never reached.

I have tried so many variations on the sheet theme, all to the same effect, I am stumped.


Solution

  • [[NSWindowController alloc] initWithWindowNibName:@"MyWindowController"]
    

    creates an instance of NSWindowController. If you want an instance of MyWindowController, use

    [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"]