8

具体来说,它在文本字段焦点方面的行为不一致。

我有一个 LSUIElement 弹出状态菜单。在该菜单中有一个包含文本字段的视图。文本字段需要是可选择的——不一定是默认选择的,但无论如何。

单击状态项时,会触发

[NSApp activateIgnoringOtherApps:YES];

它工作,大约一半的时间。* 状态菜单的另一半似乎认为自己“在后台”,即使单击它也不会让我将注意力集中在文本字段上。(我知道状态项点击触发器正在触发 b/c 上面有一个 NSLog。)

这是 Apple 处理这些状态项的方式中的错误,还是我错误处理了 activateIgnoringOtherApps?

*事实上,它似乎只是在另一个应用程序激活后第一次失败。之后它工作正常。

完整的片段:

-(void)statusItemClicked:(id)sender {
    //show the popup menu associated with the status item.
    [statusItem popUpStatusItemMenu:statusMenu];

    //activate *after* showing the popup menu to obtain focus for the text field.
    [NSApp activateIgnoringOtherApps:YES];

}
4

2 回答 2

2

终于想出了一个解决方法。

不要在点击处理程序中弹出菜单,而是激活应用程序,然后安排一个 NSTimer 没有延迟弹出菜单:

-(void)pop:(NSTimer *)timer {
    [statusItem popUpStatusItemMenu:theMenu];
}

-(void)statusItemClicked:sender {
    [NSApp activateIgnoringOtherApps:YES];
    [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(pop:) userInfo:nil repeats:NO];
}

pop:在下一帧调用,因此延迟是不可察觉的,但足够长的时间activateIgnoringOtherApps:可以做任何阻止它在同一帧中弹出菜单时按预期工作的事情。

于 2011-10-19T14:48:05.660 回答
-1

我从经验中知道,在弹出包含文本字段的菜单activateIgnoringOtherApps: 后,您必须打电话。因此,您需要按以下顺序进行操作:

- (void)statusItemClicked:sender {
    [statusItem popUpStatusItemMenu:theMenu];
    [NSApp activateIgnoringOtherApps:YES]; // FYI, NSApp is shorthand for [NSApplication sharedApplication]
}

根据您所说的,听起来您的应用程序激活太晚了,因此在您第一次单击该项目时它没有被激活,但在随后的单击中它已经被激活。

于 2009-12-07T17:50:47.183 回答