具体地说,它在文本字段焦点方面的行为不一致。
我有一个LSUIElement弹出了一个状态菜单。在该菜单中有一个包含文本字段的视图。文本字段需要是可选择的--不一定是默认选择,而是任意选择。
当单击状态项时,它会触发
[NSApp activateIgnoringOtherApps:YES];它工作,大约一半的时间。*状态菜单的另一半似乎认为自己是“在后台”,不让我把焦点放在文本字段上,即使点击它。(我知道状态项click-trigger正在触发b/c上面有一个NSLog。)
这是苹果处理这些状态项的方式上的错误,还是我对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];
}发布于 2011-10-19 22:48:06
最后想出了解决这个问题的办法。
不是在点击处理程序中弹出菜单,而是激活应用程序,然后立即安排一个弹出菜单的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:在同一帧中弹出菜单时,做任何阻止它按预期工作的事情。
发布于 2009-12-08 01:50:47
根据我的经验,在弹出包含文本字段的菜单后,必须调用activateIgnoringOtherApps:。因此,您需要按以下顺序执行此操作:
- (void)statusItemClicked:sender {
[statusItem popUpStatusItemMenu:theMenu];
[NSApp activateIgnoringOtherApps:YES]; // FYI, NSApp is shorthand for [NSApplication sharedApplication]
}根据你所说的,听起来你的应用程序激活太晚了,所以它不会在你第一次点击项目时被激活,但它已经在随后的点击中被激活了。
https://stackoverflow.com/questions/1857603
复制相似问题