LSUIElement与activateIgnoringOtherApps的行为不一致

ico*_*ter 8 cocoa statusbar nsstatusitem

具体而言,它在文本字段焦点方面表现不一致.

我有一个LSUIElement弹出一个状态菜单.在该菜单中,有一个包含文本字段的视图.文本字段需要是可选择的 - 默认情况下不一定是选中,而是以任何方式选择.

单击状态项时,将触发

[NSApp activateIgnoringOtherApps:YES];
Run Code Online (Sandbox Code Playgroud)

它的工作时间大约是一半时间.*状态菜单的另一半似乎是"在后台",并且不会让我把焦点放在文本字段上,即使点击它也是如此.(我知道状态项单击触发器正在触发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];

}
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex -1

根据经验,我知道您必须在弹出包含文本字段的菜单activateIgnoringOtherApps: 后调用。因此,您需要按以下顺序执行此操作:

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

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