4

我正在尝试使用 makeKeyAndOrderFront 从另一个窗口打开一个窗口。新窗口出现,但没有获得焦点。

主窗口的代码是:

#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]前面,makeKeyAndOrderFront但它没有任何区别。

4

3 回答 3

8

您是否确保 SecondWindowController 的窗口插座已连接到您的 NIB 中的窗口?只需加载NIB即可显示该窗口,即使插座未连接也是如此。

于 2009-10-27T16:52:10.633 回答
6

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

于 2009-10-27T06:11:17.040 回答
2

试试这个:

if (!secondWindowController)
    secondWindowController = [[SecondWindowController alloc] init];    
NSApplication *thisApp = [NSApplication sharedApplication];
[thisApp activateIgnoringOtherApps:YES];
[[secondWindowController window] makeKeyAndOrderFront:self];
于 2009-10-27T04:34:10.627 回答