gpt4 book ai didi

objective-c - 重新显示窗口后未调用 NSWindow windowDidResignKey

转载 作者:行者123 更新时间:2023-12-03 16:38:41 27 4
gpt4 key购买 nike

我有一个自定义 NSWindow 子类,用户可以通过单击按钮来切换其显示。我还希望当窗口放弃关键状态时窗口消失(例如,通过用户在窗口外部单击)。

我有一个实现了 windowDidResignKey: 的委托(delegate),但我发现这个委托(delegate)方法仅在窗口第一次退出键时被调用。

以下是我切换窗口显示的方法(通过用户操作或 windowDidResignKey):

- (void) toggleWindowAtPoint:(NSPoint)point
{
// Attach/detach window.
if (!attachedWindow)
{
attachedWindow = [[CustomWindow alloc] attachedToPoint:point];
attachedWindow.delegate = self;
[attachedWindow setLevel:NSMainMenuWindowLevel+1]; // show window in front of all other apps on desktop
[attachedWindow makeKeyAndOrderFront:self];
}
else
{
attachedWindow.delegate = nil;
[attachedWindow orderOut:self];
[attachedWindow release];
attachedWindow = nil;
}
}

这是我的 windowDidResignKey 实现:

- (void) windowDidResignKey:(NSNotification *)note
{
[self toggleWindowAtPoint:NSMakePoint(0, 0)];
}

我发现第一次显示自定义窗口时,windowDidResignKey: 被调用。此后每次重新显示自定义窗口时,都不会调用 windowDidResignKey:

最佳答案

问题是,在某些情况下,调用 [attachedWindow makeKeyAndOrderFront:self] 后,自定义窗口实际上并未成为关键窗口。 .

我通过在重新创建窗口之前添加以下行来修复此问题:

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

在上面的代码片段的上下文中:

- (void) toggleWindowAtPoint:(NSPoint)point
{
// Attach/detach window.
if (!attachedWindow)
{
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
attachedWindow = [[CustomWindow alloc] attachedToPoint:point];
....

关于objective-c - 重新显示窗口后未调用 NSWindow windowDidResignKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6737396/

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