27

makeKeyAndOrderFront is not making my NSWindow key or front.

My app does not have a main window or menubar, which may be part of the problem?

IBOutlet NSWindow *loginWindow;
//(connected in Interface Builder to the NSWindow)

The NSWindow has "visible at launch" and "release when closed" both unchecked.

Then:

- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [loginWindow makeKeyAndOrderFront:self];
}

This does open the window. (commenting it out results in no window being opened).

However:

  • It appears behind the Xcode window (not front).

  • It appears to recieve mouse focus, but will not take keypresses in the textfields the window contains. The keypresses are sent to Xcode.

  • It appears in the Expose grid when Expose is activated. But I cannot click the window to select it in Expose... it won't come to the front.

Why isn't my Window working?

ck_
  • 3,719
  • 10
  • 49
  • 76
  • 3
    Applications aren't required to have a “main window”, or indeed any application-specific windows at all, nor a main menu (though they always should have a main menu—a lot of key commands come for free that way, even if the application does not use the menu bar). Whatever window has the active appearance is the Main Window in Cocoa terminology; more importantly, it is completely valid for an application to have no windows, as demonstrated by every document-based application, background-only application, and UI element. – Peter Hosey Sep 18 '11 at 08:08

3 Answers3

37

Try calling this method [NSApp activateIgnoringOtherApps:YES];. This should make it the active application.

ms83
  • 1,804
  • 16
  • 13
  • Exactly as you wrote? Or do I have to substitute a variable in for `NSApp`? Pasted in as is, above the `makeKeyAndOrderFront` line, there's no change. Exact same behavior as I described above. – ck_ Sep 18 '11 at 08:00
  • @cksubs: `NSApp` is a variable; you don't need to change it. This should solve your immediate problem (the application is not frontmost), but shouldn't be necessary (the application should have come frontmost automatically as part of Xcode launching it). – Peter Hosey Sep 18 '11 at 08:04
  • 3
    P.S. I actually did end up needing to use this `activateIgnoringOtherApps` command, in addition to adding `LSUIElement` to the Info.plist. – ck_ Sep 19 '11 at 02:03
  • I think you shouldn't use this method, because Microsoft has a patent for intrusive popups. "method for unnerving a computer user through a popup window that steals his focus." therefore mac-apps don't use this feature and are somewhat more introverted. – Michael Apr 19 '14 at 10:54
34

Stab in the dark: You have LSBackgroundOnly set in your Info.plist. That's what makes this not work: A background-only application cannot come to the foreground, which is why your window does not come to the foreground.

If you want your app to not show up in the Dock, set LSUIElement instead. This suppresses your app's Dock tile and keeps it from showing its own main menu in the menu bar, but preserves its ability to bring a window frontmost and make it key.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Yep, `LSUIElement` fixes it. I was using `NSBGOnly` which appears to now be deprecated. – ck_ Sep 18 '11 at 08:16
  • 2
    @cksubs: Yes: `NSBGOnly` was renamed to `LSBackgroundOnly`. Only the old name is deprecated; `LSUIElement` and `LSBackgroundOnly` are both current, but do different things. They, and all the other current keys, are documented in the Information Property List Key Reference: http://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/ – Peter Hosey Sep 18 '11 at 08:23
7

Both of these answers are correct. You'll also need to make sure you override canBecomeKey in your window subclass if it's borderless.

This took be forever to figure out. (I wrote a blog post about the entirety of my solution.)

Sam Soffes
  • 14,831
  • 9
  • 76
  • 80