tags:

views:

265

answers:

3

I am trying to open one window from another using makeKeyAndOrderFront. The new window appears, but does not receive focus.

The code for the main window is:

#import "SecondWindowController.h"
@implementation FirstWindowController
-(IBAction)showSecondWindow:(id)sender
{
  if (!secondWindowController)
    secondWindowController = [[SecondWindowController alloc] init];
  [[secondWindowController window] makeKeyAndOrderFront:self];
}

SecondWindowController is a NSWindowController, as follows:

@implementation SecondWindowController
-(id)init
{
  if (![super initWithWindowNibName:@"SecondWindow"])
    return nil;
  return self;
}

I've also tried putting [secondWindowController showWindow:self] before the makeKeyAndOrderFront but it doesn't make a difference.

+1  A: 

Try this:

if (!secondWindowController)
    secondWindowController = [[SecondWindowController alloc] init];    
NSApplication *thisApp = [NSApplication sharedApplication];
[thisApp activateIgnoringOtherApps:YES];
[[secondWindowController window] makeKeyAndOrderFront:self];
psychotik
This makes no difference, still appears in front, but without focus.
mattdwen
Oh, no focus? How about [window makeFirstResponder:nil]?
psychotik
+1  A: 

Are you using a borderless window? If so you need to override canBecomeKeyWindow and return YES

Leibowitzn
I'm not sure what a borderless window is, so I haven't intentionally created one. However, canBecomeKeyWindow does return NO. To override this, do I need to subclass NSWindow for that instance?
mattdwen
Yea, you have to override the NSWindow class.
Leibowitzn
Actually, now I think that Doug Richardson is right. Your [secondWindowController window] is probably nil!!!!!
Leibowitzn
+3  A: 

Did you make sure the window outlet for SecondWindowController is hooked up to the window in your NIB? The window could be displayed just by loading the NIB, even if the outlet is not hooked up.

Doug Richardson
Good point! This could definitely be the case.
Leibowitzn
Yeah, you got it. I had set the delegate to the File's Owner object, but hadn't set the window the other way. Is this correct to change the class of File's Owner to SecondWindowController?
mattdwen
Yes, set the class of File's Owner to SecondWindowController.
Doug Richardson