简体   繁体   English

NSApplication 在应用程序开关上使窗口位于前面

[英]NSApplication make window front on app switch

When run my app(OSX, macOS SDK 10.15), it shows the main window, as expected.运行我的应用程序(OSX,macOS SDK 10.15)时,它会按预期显示主窗口。 I then CMD-Tab to another app, but when I CMD-Tab back to my app, it won't show the window.然后我 CMD-Tab 到另一个应用程序,但是当我 CMD-Tab 回到我的应用程序时,它不会显示窗口。 The same happens if I click on it in the dock.如果我在 Dock 中单击它,也会发生同样的情况。 I've tried various suggestions, such as:我尝试了各种建议,例如:

[NSApp activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:nil];
[window orderFrontRegardless];

and

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationSwitchedTo) name:NSWorkspaceDidActivateApplicationNotification object:nil];

-(void)applicationSwitchedTo
{
    [window makeKeyAndOrderFront:nil];
}

I've set up all the callback methods in both the window and app delegates, but I don't get anything,except the above callback, when I switch to or from my app.我已经在窗口和应用程序委托中设置了所有回调方法,但是当我切换到我的应用程序或从我的应用程序切换时,我没有得到任何东西,除了上面的回调。 I would think it would default behavior to show the apps main window when switched to it from another app.我认为从另一个应用程序切换到应用程序主窗口时显示应用程序主窗口是默认行为。 Any help is greatly appreciated!任何帮助是极大的赞赏!

I figured out a fix, but I'm not sure if it's the proper thing to do.我想出了一个解决办法,但我不确定这是否是正确的做法。 I noticed that if I called我注意到如果我打电话

[NSApp finishLaunching]

then the window ordering functions don't work.那么窗口排序功能不起作用。 I don't need it's functionality, so I removed it.我不需要它的功能,所以我删除了它。 Furthermore, I'm now handling the event type NSEventTypeAppKitDefined, which is called when you switch on and off the app.此外,我现在正在处理事件类型 NSEventTypeAppKitDefined,它在您打开和关闭应用程序时调用。 I then check the subtype and call the window ordering function:然后我检查子类型并调用窗口排序函数:

case NSEventTypeAppKitDefined:
{
     if([theEvent subtype] == NSEventSubtypeApplicationActivated) {
          [window makeKeyAndOrderFront:nil];
     }
}

Hope this helps someone down the line!希望这可以帮助某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM