开发者

makeKeyAndOrderFront only does the latter

开发者 https://www.devze.com 2022-12-10 00:51 出处:网络
I am trying to open one window from another using makeKeyAndOrderFront.The new window appears, but does not receive focus.

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.


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.


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


Try this:

if (!secondWindowController)
    secondWindowController = [[SecondWindowController alloc] init];    
NSApplication *thisApp = [NSApplication sharedApplication];
[thisApp activateIgnoringOtherApps:YES];
[[secondWindowController window] makeKeyAndOrderFront:self];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号