jdelaune
jdelaune

Reputation: 161

Modal Window in Cocoa

I'm trying to create a custom modal window and here is the code I have so far:

NSWindowController *modalSheet = [[NSWindowController alloc]
initWithWindowNibName:@"MyCustomWindow" owner:self];

[NSApp beginSheet:[modalSheet window]
 modalForWindow:[self windowForSheet]
  modalDelegate:nil
 didEndSelector:nil
    contextInfo:nil];

The window pops up fine but it's not modal e.g. you can still do things to the parent window where the requests come from. This method is called from an NSDocument object.

I've tried to read: Using Custom Sheets

However i'm not sure what myCustomSheet is as it's not declared anywhere. I assume it's an NSWindow instance variable.

I just can't understand why it's not modal. Any help would be much appreciated. Thanks

Upvotes: 0

Views: 12742

Answers (3)

Nuzhdin Vladimir
Nuzhdin Vladimir

Reputation: 1822

Hope this helps. To run your window controller always modal. Create your own subclass of NSWindowController and add this two methods.

- (void)windowDidLoad
{
    [super windowDidLoad];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{//TODO
        [[NSApplication sharedApplication] runModalForWindow:self.window];
    });
}

- (void)windowWillClose:(NSNotification *)notification
{
    [[NSApplication sharedApplication] stopModal];
}

Upvotes: 7

d11wtq
d11wtq

Reputation: 35298

Have you by any chance got worksWhenModal set to YES? This would cause exactly the behaviour you describe, since that's the purpose of it.

With regards to wrapping the panel (you're using a NSPanel, right?) in it's own window controller, you don't need to. I actually just make my sheets without a window controller. Do you have any custom logic in the window controller that you need? AppKit handles it for you.

Upvotes: 0

John Parker
John Parker

Reputation: 54435

There's a section of Apple's Window Programming Guide dedicated specifically to Using Modal Windows which tells you pretty much everything you should need.

Upvotes: 3

Related Questions