Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a Menu Bar (NSPopover) App active when clicking on the menu button?

I have an app that sits in the menu bar, and when you click on the button it presents an NSPopover.

That all works fine, but I want an NSSearchField to become the first responder, so a user can then type straight in the box.

However, if the app was not already selected, it won't work. And although it looks like the search box is active, the keyboard input goes to the previous app that was open, because technically it is still the active window.

I've seen a few questions about things like this, but they all require objc methods like makekeyandorder, or just stuff that won't apply to an NSPopover.

So my question is - Is there a way that when the button is pressed, I can then force the application to become the active app?

I had a wild guess and tried NSApplication().sharedApplication().becomeFirstResponder(), but no luck.

If there's another way to do it, that I've just missed completely, then please let me know!

like image 660
Christopher Hannah Avatar asked Jun 15 '16 23:06

Christopher Hannah


1 Answers

I fixed it myself in the end, it turned out to be a very simple fix as expected.

All I needed to to was call NSApplication.sharedApplication().activateIgnoringOtherApps(true) in the ViewControllers viewDidAppear() method.

It fixes everything!

like image 92
Christopher Hannah Avatar answered Sep 30 '22 09:09

Christopher Hannah