cocoa - LSUIElement 的行为与 activateIgnoringOtherApps 不一致

标签 cocoa statusbar nsstatusitem

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

我有一个 LSUIElement 弹出状态菜单。该菜单中有一个包含文本字段的 View 。文本字段需要是可选择的——不一定是默认选择的,但可以选择任意一个。

当状态项被点击时触发

[NSApp activateIgnoringOtherApps:YES];

大约有一半的时间它可以工作。* 状态菜单的另一半似乎认为自己“在后台”,即使单击它也不会让我将焦点放在文本字段上。 (我知道状态项点击触发正在触发,因为上面有一个 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];

}

最佳答案

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

不要在点击处理程序中弹出菜单,而是激活应用程序,然后安排一个 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: 执行弹出菜单时阻止其按预期工作的任何操作在同一帧中。

关于cocoa - LSUIElement 的行为与 activateIgnoringOtherApps 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1857603/

相关文章:

cocoa - 自动创建冲突的 NSFileVersions

android - 构建像纽约时报应用程序一样的 Android 状态栏

macos - NSStatusItem 无法通过键盘导航

swift - 如何读取 Xcode (Swift) 中 OS X 菜单栏的用户选择

iphone - 获取随机 NSDictionary 键?

objective-c - 类似 nswindow 级别的 growl 通知

objective-c - Window 总是想领先于其他人 - Xcode

c++ - 更改状态栏位置

.net - 我可以在让用户等待之前更新 WPF 状态栏文本吗?

objective-c - 在 Objective-C/Cocoa 中,当激活任务控制或更改空间时是否有全局事件?