我正在为我想要使用NSFontPanel的应用程序创建一个特性。我不想在我的应用程序中有一个“字体”菜单。
单击菜单项时打开和关闭字体面板的过程如下所示
- (IBAction) showOverlayControls:(id)sender
{
if ( [[NSFontPanel sharedFontPanel] isVisible])
{
NSLog(@"Test");
[[NSFontPanel sharedFontPanel] orderOut:self];
}
else
{
NSFontManager* fontMgr = [NSFontManager sharedFontManager];
[fontMgr setTarget:self];
NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel];
[fontPanel orderFront:self];
}
}它工作正常。当我试图在应用程序启动时关闭字体面板,以防它被显示时,问题就出现了。我试过了
if ( [[NSFontPanel sharedFontPanel] isVisible] )
[[NSFontPanel sharedFontPanel] close];或
if ( [[NSFontPanel sharedFontPanel] isVisible] )
[[NSFontPanel sharedFontPanel] orderOut:self];我也尝试过没有if语句,仍然没有成功。如果在关闭应用程序时显示面板,则在打开应用程序时总是会再次弹出面板。我还试图关闭我的应用程序委托的appWillTerminate方法中的字体面板。同样的行为。
如果有任何提示将不胜感激。提前谢谢你,
浮点
发布于 2012-09-01 04:18:30
你在哪里调用这些方法?它必须起作用。
您可以像这样在AppDelegate -applicationDidFinishLaunching:通知中调用它:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
if ([[NSFontPanel sharedFontPanel] isVisible])
[[NSFontPanel sharedFontPanel] orderOut:self];
}发布于 2021-09-26 01:11:16
如果应用程序在NSFontPanel或NSColorPanel仍然可见时关闭,此解决方案可能会有所帮助。将以下代码添加到您的AppDelegate类中,以避免在应用程序启动时恢复NSFontPanel或NSColorPanel窗口。感谢https://christiantietze.de/posts/2019/06/observe-nswindow-changes/提供了一种检测窗口添加时间的方法。
func applicationDidUpdate(_ notification: Notification) {
NSApp.windows
.filter { ["NSFontPanel", "NSColorPanel"].contains($0.className) }
.filter { $0.isVisible }
.forEach { $0.isRestorable = false }
}https://stackoverflow.com/questions/12140480
复制相似问题