Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa switching a focus to my application

I have a statusbar application (the one that runs in the top bar of the system). When i click on the icon in the statusbar, a menu pops up and it has an item to configure my app. When i press this item, the main app window is shown [wndMain makeKeyAndOrderFront:self]; The problem is clicking a statusbar icon doesn't activate the application and, if other application is active at that moment, the main window will open below that active application and will be out of focus. How can i programatically switch focus to my application when clicking on the statusbar icon so that the settings window would always open on top of other windows and would be focused and ready for user input?

Thank you

like image 802
Marius S. Avatar asked Feb 10 '10 08:02

Marius S.


Video Answer


2 Answers

Send the shared NSApplication object an activateIgnoringOtherApps: message.

like image 143
Peter Hosey Avatar answered Sep 20 '22 09:09

Peter Hosey


NSApplication *myApp = [NSApplication sharedApplication];
[myApp activateIgnoringOtherApps:YES];
[self.window orderFrontRegardless];

you can use the code above to solve the problem.

like image 42
6 1 Avatar answered Sep 23 '22 09:09

6 1