Search code examples
objective-ccocoamacosnibnsbundle

Loading NIB using [NSBundle loadNibNamed:owner:] but the window does not appear in the foreground


I wrote a menu application that has no persistent window or standard menu. When another application has focus and I use the menulet to trigger a window to be opened, it appears behind the foreground application (but above anything else that is present on the screen).

Basically...

-(IBAction)aboutWindow:(id)sender {
    [NSBundle loadNibNamed:@"About" owner:self];
}

Can anyone point me in the right direction so I can get this window to appear above all other applications when it is initially spawned?

[Edit]

I have tried using a custom NSWindowController with the window linked up, and awakeFromNib calling a makekeyandorderfront method, but that wasn't doing anything.

I now have instead of the NSBundle call:

NSWindowController* awc = [[NSWindowController alloc] initWithWindowNibName:@"About"];
[[awc window] makeKeyAndOrderFront:nil];

And that spawns the window, but still does not make it in the foreground


Solution

  • Figured it out. Nothing was wrong with the Window, it was the Application. It was not in the foreground because of its nature as a menulet with no windows before this one is spawned. Final code:

    -(IBAction)aboutWindow:(id)sender {
        NSWindowController* awc = [[NSWindowController alloc] initWithWindowNibName:@"About"];
        [[awc window] makeKeyAndOrderFront:nil];
        [[NSApplication sharedApplication] arrangeInFront:nil];
    }