【问题标题】:Prevent custom sheet from snatching focus防止自定义工作表抢焦点
【发布时间】:2012-05-21 01:23:16
【问题描述】:

我正在使用 Cocoa 编写一个多文档应用程序。用户在打开文档时必须输入密码。在一段时间内没有对文档进行任何操作后,再次要求用户输入密码。

现在我正在使用NSAplicationbeginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: 在自定义工作表中显示密码提示。虽然它有效,但不幸的是,即使当时正在处理另一个文档,窗口也会被带到前面并获得焦点。只有当我的应用程序在前面时才会有问题。

如果父窗口不活动,有没有办法防止打开工作表抢焦点?

【问题讨论】:

  • 可能会延迟显示工作表,直到窗口成为关键?

标签: objective-c cocoa cocoa-sheet


【解决方案1】:

没有简单的方法。 hacky 的方法是为文档的窗口 工作表的窗口创建一个 NSWindow 的子类,并在该类中覆盖 orderFront: 和 makeKeyWindow 在调用 beginSheet 期间什么都不做。例如,

在 NSWindow 子类中:

-(void)awakeFromNib
{
    hack = NO;
}

-(void)hackOnHackOff:(BOOL)foo
{
    hack = foo;
}

- (void)orderFront:(id)sender
{
    if (!hack)
        [super orderFront:sender];
}

- (void)makeKeyWindow
{
    if (!hack)
        [super makeKeyWindow];
}

然后您的 beginSheet 调用将如下所示:

-(void)sheet
{
    SpecialSheetWindow* documentWindow = [self windowForSheet];
    [documentWindow hackOnHackOff:YES];
    [sheetWindow hackOnHackOff:YES];
    [[NSApplication sharedApplication] beginSheet:sheetWindow
                       modalForWindow:documentWindow 
                       modalDelegate:self  didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:nil];
    [documentWindow hackOnHackOff:NO];
    [sheetWindow hackOnHackOff:NO];
}

【讨论】:

  • 是的,这确实是一个 hack,我宁愿避免走这条路。我想我将结合使用 Ken T. 的建议,仅在窗口成为关键时才显示工作表,并为用户提供视觉提示(可能是半透明的叠加层)。非常感谢你们!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 2013-11-04
  • 2011-02-08
  • 2016-09-12
相关资源
最近更新 更多