开发者

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开发者_如何学Python'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


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];
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜