我在NSTextField上右键单击时显示上下文菜单。 我正在使用以下代码在我的NSTextField的rightMouseDown事件中弹出上下文菜单:
- (void) rightMouseDown:(NSEvent*)theEvent
{
NSMenu* theMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
[theMenu insertItemWithTitle:@"Suggest Link/Movie" action:@selector(openSuggestionMovieLink) keyEquivalent:@"" atIndex:0];
[theMenu setDelegate:self];
[NSMenu popUpContextMenu:theMenu withEvent:theEvent forView:self];
}
我需要停止在用户单击窗口的任何位置时关闭此上下文菜单。
我尝试通过覆盖窗口的鼠标按下事件并在打开上下文菜单时不发送[super mousedown:event]调用来执行相同操作。这没有起作用。
我还尝试使用NSMenu代理方法menuDidClose:并再次在其中打开菜单。
- (void)menuDidClose:(NSMenu *)menu
{
NSLog(@"close");
[NSMenu popUpContextMenu:menu withEvent:nil forView:self];
}
但是对我来说什么都没用。
有没有什么方式可以做到相同的效果。请指点我正确的方向。 我会感激任何帮助。谢谢。