Cocoa MakeKeyandDerfront只执行后者

Cocoa MakeKeyandDerfront只执行后者,cocoa,nswindow,Cocoa,Nswindow,我正在尝试使用MakeKeyandDerfront从一个窗口打开另一个窗口。新窗口出现,但不接收焦点 主窗口的代码为: #import "SecondWindowController.h" @implementation FirstWindowController -(IBAction)showSecondWindow:(id)sender { if (!secondWindowController) secondWindowController = [[SecondWindowCo

我正在尝试使用MakeKeyandDerfront从一个窗口打开另一个窗口。新窗口出现,但不接收焦点

主窗口的代码为:

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

@implementation SecondWindowController
-(id)init
{
  if (![super initWithWindowNibName:@"SecondWindow"])
    return nil;
  return self;
}
我也尝试过将
[secondWindowController showWindow:self]
放在
MakeKeyandDerfront
之前,但这没有什么区别。

试试这个:

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

您使用的是无边界窗口吗?如果是这样,您需要覆盖canBecomeKeyWindow并返回YES

您是否确保SecondWindowController的窗口插座连接到NIB中的窗口?即使插座未连接,也可以通过加载笔尖来显示窗口。

这没有区别,仍然显示在前面,但没有焦点。哦,没有焦点?那么[WindowMakeFirstResponder:nil]呢?我不确定什么是无边界窗口,所以我没有特意创建一个。但是,canBecomeKeyWindow确实返回NO。要覆盖它,我需要为那个实例创建NSWindow子类吗?是的,你必须覆盖NSWindow类。事实上,现在我认为Doug Richardson是对的。您的[secondWindowController窗口]可能为零!!!!!无边框窗口是NSWindow.styleMask=NSWindow.styleMask.borderless的NSWindow。我不知道Objective-C的正确语法。我已经将委托设置为文件的所有者对象,但没有将窗口设置为其他方式。将文件所有者的类别更改为SecondWindowController是否正确?是,将文件所有者的类别设置为SecondWindowController。