Cocoa 中的模态窗口

发布于 2024-10-09 14:53:12 字数 664 浏览 2 评论 0原文

我正在尝试创建一个自定义模态窗口,这是迄今为止我拥有的代码:

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

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

窗口弹出得很好,但它不是模态的,例如您仍然可以对请求来自的父窗口执行操作。该方法是从 NSDocument 对象调用的。

我尝试阅读: 使用自定义工作表

但是我不确定 myCustomSheet 是什么,因为它没有在任何地方声明。我假设它是一个 NSWindow 实例变量。

我只是不明白为什么它不是模态的。任何帮助将不胜感激。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风柔一江水 2024-10-16 14:53:12

希望这有帮助。
始终以模态方式运行窗口控制器。创建您自己的 NSWindowController 子类并添加这两个方法。

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

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];
}
够钟 2024-10-16 14:53:12

Apple 的 Window 编程指南中有一节专门介绍 使用模态窗口它告诉你几乎所有你应该需要的东西。

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.

风铃鹿 2024-10-16 14:53:12

您是否有机会将 worksWhenModal 设置为 YES?这将导致完全您所描述的行为,因为这就是它的目的。

关于将面板(您使用的是 NSPanel,对吧?)包装在它自己的窗口控制器中,您不需要这样做。实际上我只是在没有窗口控制器的情况下制作床单。您在窗口控制器中是否有您需要的自定义逻辑? AppKit 会为你处理它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文