6

我在我的 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];
}

但没有什么对我有用。

有没有办法做同样的事情。请指出我正确的方向。我会很感激任何帮助。谢谢。

4

1 回答 1

0

您是否尝试过继承 NSMenu 并覆盖

- (void)cancelTracking;
- (void)cancelTrackingWithoutAnimation;

修改以适应您的自定义行为?怎么了?

于 2013-11-06T12:40:23.197 回答