2

I am programmatically creating an NSMenu with a NSMenuItem. When the window of the application is active, the NSMenuItem is enabled:

Enabled NSMenuItem

However, as soon as the window loses focus the menu item becomes disabled:

Disabled NSMenuItem

Here's how I am creating the NSMenu:

- (void)_quit
{
  [[NSApplication sharedApplication] terminate:nil];
}

- (NSMenu *)_setupMenu
{
  NSMenu *statusMenu = [[NSMenu alloc] initWithTitle:@"Demo"];
  NSMenuItem *quit = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(_quit) keyEquivalent:@""];

  [statusMenu addItem:quit];

  return statusMenu;
}

What is causing this issue? And how do I go about making it enabled regardless of whether the application is in focus or not?

4

1 回答 1

3

因为菜单项是基于响应者链启用的。

在您的情况下,您可以使用terminate:选择器而不是您自己的。
由于这是在NSApplication类中声明的,它也是响应者链的一部分,因此该项目将始终处于活动状态。

NSMenuItem *quit = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@""];

更多信息:Cocoa 事件处理指南

于 2013-07-20T23:35:09.733 回答