Snow Leopard 上的窗口关闭按钮劫持

发布于 2024-12-22 12:53:51 字数 527 浏览 0 评论 0原文

我正在开发一个基于 NSDocument 的应用程序,每个窗口(选项卡)有多个文档。这意味着我需要自己处理窗口的关闭,以便我可以在窗口关闭之前查看属于该窗口的文档。为了做到这一点,我使用 standardWindowButton:NSWindowCloseButton 访问 NSWindow 的关闭按钮,并将该按钮的目标/操作设置为我的方法,而不是标准(和私有)_close:方法。

这在 Lion 上效果很好,但在 Snow Leopard 上却会引起问题。每当显示模式对话框时,关闭按钮就会按预期禁用。但是,当模式对话框被关闭时,在 Snow Leopard 上,关闭按钮永远不会重新启用。我尝试使用 [closeButton setEnabled:YES] 等以编程方式重新启用它,但它似乎没有任何效果。我已经确认,只有当我更改关闭按钮的目标/操作时才会发生这种情况。

关于如何避免 Snow Leopard 上的这种行为的任何想法,或者劫持关闭按钮的替代方法?控制工具栏按钮的启用状态的是什么?也许我可以覆盖那里的东西?

I'm working on an NSDocument-based app with multiple documents per window (tabs). This means I need to handle closing of windows myself so that I can review the documents belonging to the window before it's closed. In order to do this, I've accessed the NSWindow's close button using standardWindowButton:NSWindowCloseButton and set the target/action of this button to my method instead of the standard (and private) _close: method.

This works great on Lion, but on Snow Leopard it causes issues. Whenever a modal dialog is displayed, the close button gets disabled as expected. But when the modal dialog is dismissed, on Snow Leopard the close button never gets reenabled. I've tried reenabling it programmatically afterwards using [closeButton setEnabled:YES] etc, but it doesn't seem to have any effect. I've confirmed that this only happens when I've changed the target/action of the close button.

Any ideas on how to avoid this behavior on Snow Leopard, or maybe an alternative way of hijacking the close button? What is it that controls the enabled state of the toolbar buttons? Maybe I could override something there?

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

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

发布评论

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

评论(1

↘人皮目录ツ 2024-12-29 12:53:51

我本以为你可以使用 windowShouldClose: 委托方法

将 windows 委托设置为 AppDelegate。在 AppDelegate 中,使用 windowShouldClose: 委托方法来调用 close 方法,并通过返回 NO 来停止窗口关闭。
在您的方法中进行所有检查,然后执行Close:窗口。看我的例子

 NSWindow * thisWindow; //--pointer to window that will be closed
BOOL windowClose;//-- bool for confirming close of window.

- (BOOL)windowShouldClose:(id)sender{
    thisWindow =sender;//-- set thisWindow to the sender window,the one that is to be closed )
    //if (sender ==theWindow) {//--you can use this to do further checking

        if (windowClose) {//-- Close window if YES
            return YES;  
        } 
    //}


    [self performSelector:@selector(myCloseWindow) ];//go to your method
    windowClose =FALSE;//-- reset
    return NO;//do not close window here
}

- (void) myCloseWindow {
    NSLog(@"closing window");//-- do your stuff
    windowClose =TRUE;//--give the ok to close the window
    [thisWindow performClose:thisWindow];//-- perform the close, which will be redirected back to the delegate, which will now allow the window to close
}

I would have thought you could use windowShouldClose: delegate method

Set the windows delegate to the AppDelegate. And in the AppDelegate use the windowShouldClose: delegate method to call your close method and to stop the window closing by returning NO.
In your method do all your checking and then performClose: the window. See my example

 NSWindow * thisWindow; //--pointer to window that will be closed
BOOL windowClose;//-- bool for confirming close of window.

- (BOOL)windowShouldClose:(id)sender{
    thisWindow =sender;//-- set thisWindow to the sender window,the one that is to be closed )
    //if (sender ==theWindow) {//--you can use this to do further checking

        if (windowClose) {//-- Close window if YES
            return YES;  
        } 
    //}


    [self performSelector:@selector(myCloseWindow) ];//go to your method
    windowClose =FALSE;//-- reset
    return NO;//do not close window here
}

- (void) myCloseWindow {
    NSLog(@"closing window");//-- do your stuff
    windowClose =TRUE;//--give the ok to close the window
    [thisWindow performClose:thisWindow];//-- perform the close, which will be redirected back to the delegate, which will now allow the window to close
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文