set NSWindow focused

て烟熏妆下的殇ゞ 提交于 2019-12-10 19:34:41

问题


I have an app winth one window and one panel, attached to this window.

steps:

  1. deactivate my app (app opened, but without the focus)
  2. click on a button on panel (panel is focused now, but main window is not)

How to set focus to the main window (parent window) from the panel?


回答1:


It is not clear what you mean by focus, and whether what you call main window is a main window as defined in Cocoa. Assuming it is a Cocoa main window and focus is the same as key status,

[[NSApp mainWindow] makeKeyWindow];

or

[[NSApp mainWindow] makeKeyAndOrderFront:self];

If it is not a Cocoa main window, you need to have a reference to it and send it -makeKeyWindow or -makeKeyAndOrderFront:.




回答2:


Swift 5 version of @BUDDAx2 answer:

NSApplication.shared.activate(ignoringOtherApps: true)


来源:https://stackoverflow.com/questions/5089598/set-nswindow-focused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!