gpt4 book ai didi

objective-c - 如何检测 NSPanel 何时因失去焦点而关闭?例如。用户在 NSPanel 外部单击(窗口?)

转载 作者:行者123 更新时间:2023-12-03 17:51:28 25 4
gpt4 key购买 nike

我有一个通用的 NSPanel 窗口,我将其用作应用程序中的首选项窗口。我有一个选择器,每次窗口关闭时都会调用它。该选择器的目的是保存用户选择的首选项的状态(没有“保存”按钮)。

我有一个 NSButton(“CLOSE”),我可以轻松设置它来调用我的关闭选择器。

我将其设置为当用户单击 NSPanel 左上角的 RED X 时也会调用我的选择器:

NSButton *closeButton = [[self window] standardWindowButton:NSWindowCloseButton];
[closeButton setTarget:self];
[closeButton setAction:@selector(myCloseSelector:)];

这非常有效。我的问题呢?如果用户点击 NSPanel 的外部,窗口也会关闭。例如。如果他们拿起鼠标并单击弹出的 NSPanel 下方的浏览器窗口。这也会关闭窗口。

如何捕捉 NSPanel 失去焦点并关闭的情况?我需要确保当发生这种情况时我也会调用我的选择器。

谢谢!

最佳答案

使我的 NSWindowController 成为 NSWindowDelegate 的委托(delegate)。

myWindowController.h

@interface myWindowController : NSWindowController <NSWindowDelegate>

然后将 myWindowController 设置为 NSPanel 的委托(delegate)。

现在我可以实现:

- (void) windowDidResignKey:(NSNotification *)notification {
NSLog(@"Houston...we lost a panel.");
}

一切都很顺利!

关于objective-c - 如何检测 NSPanel 何时因失去焦点而关闭?例如。用户在 NSPanel 外部单击(窗口?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25556867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com