gpt4 book ai didi

macos - NSWindow 关闭后 activateIgnoringOtherApps 和 makeKeyAndOrderFront 不起作用

转载 作者:行者123 更新时间:2023-12-03 16:28:21 32 4
gpt4 key购买 nike

我从一个 Storyboard项目开始。我把菜单栏项目。单击菜单栏项时,会在 AppDelegate.swift 中触发以下方法。

func setWindowVisible() {
NSApp.activateIgnoringOtherApps(ture)
NSApp.mainWindow?.makeKeyAndOrderFront(self)
}

这使我的应用程序位于前面。但是一旦我单击关闭按钮(窗口上的红色按钮),它就永远不起作用。

无论我关闭窗口,它都可以在基于 Non-Storyboard 的项目中工作。

我已经设置

NSApp.mainWindow?.releasedWhenClosed = false

在 applicationDidFinishLaunching() 中

有人可以帮我吗?

最佳答案

applicationDidFinishLaunching中设置releasedWhenClosed没有任何效果,因为此时mainWindow属性为nil。 → 执行该方法后将创建窗口。

在 Interface Builder 中创建窗口时,releasedWhenClosed 默认为 false。

关闭窗口后,mainWindow 属性可能为零,因为这样就不再有 mainWindow 了。来自文档:

The value in this property is nil when the app’s storyboard or nib file has not yet finished loading. It might also be nil when the app is inactive or hidden.

通过从 NSAppwindows 数组中访问该窗口,我能够再次显示该窗口(关闭后)。

NSApp.activateIgnoringOtherApps(true)
NSApp.windows[0].makeKeyAndOrderFront(self)

如果您有多个窗口,您需要在数组中找到正确的窗口..

关于macos - NSWindow 关闭后 activateIgnoringOtherApps 和 makeKeyAndOrderFront 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37521593/

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