NSMenuItem 未启用?
我有一个名为“历史”的 NSMenuItem,它驻留在一个名为“菜单”的 NSMenu 中。当我的程序启动时,历史记录没有子菜单,因此它被禁用。然后在某个时候,我需要一个历史记录的子菜单,所以我创建它,使其成为历史记录的子菜单。历史记录旁边会出现一个箭头,告诉我子菜单就在那里。但历史仍然被禁用。我尝试过 setEnabled 但不起作用。请帮忙。这是我的代码:
这是我创建菜单和历史记录的时间,正如您可以在菜单中看到 NSMenuItem 一样。
menu = [[NSMenu alloc] initWithTitle:@"Menu"];
[[menu addItemWithTitle:@"History" action:nil keyEquivalent:@""] setTarget:self];
[[menu addItemWithTitle:@"Settings" action:@selector(loadSettings:) keyEquivalent:@""] setTarget:self];
[[menu addItemWithTitle:@"Quit" action:@selector(terminateApp:) keyEquivalent:@""] setTarget:self];
此时,历史记录被禁用(灰显)。 然后在程序中的某个地方我需要有一个历史记录子菜单,这样:
if (historyMenu == nil) {
historyMenu = [[NSMenu alloc] initWithTitle:@"Lyrics history"];
[menu setSubmenu:historyMenu forItem:[menu itemWithTitle:@"History"]];
}
我现在在历史记录旁边看到一个箭头,但它仍然是灰色的。
请帮忙,过去两个小时我一直在试图解决这个问题。谢谢。
I have an NSMenuItem called history which resides in a NSMenu called menu. When my program starts history doesn't have a submenu so its disabled. Then at some point, I need a submenu for history, so I create it, make it a submenu of history. An arrow appears beside history which tells me that the submenu is there. But history is still disabled. I have tried setEnabled but doesn't work. Please help. Here my code:
This is when I create my menu, and history as you can see an NSMenuItem in menu.
menu = [[NSMenu alloc] initWithTitle:@"Menu"];
[[menu addItemWithTitle:@"History" action:nil keyEquivalent:@""] setTarget:self];
[[menu addItemWithTitle:@"Settings" action:@selector(loadSettings:) keyEquivalent:@""] setTarget:self];
[[menu addItemWithTitle:@"Quit" action:@selector(terminateApp:) keyEquivalent:@""] setTarget:self];
At this point, history is disabled (greyed out).
Then somewhere in the program I need to have a submenu for history so:
if (historyMenu == nil) {
historyMenu = [[NSMenu alloc] initWithTitle:@"Lyrics history"];
[menu setSubmenu:historyMenu forItem:[menu itemWithTitle:@"History"]];
}
I now see an arrow beside history, but it's still greyed out.
Please help, I have been trying to figure this out for last 2 hours. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在设定目标,但没有采取任何行动。尝试不设定目标。这可能会使其始终处于启用状态,在这种情况下,您可能必须手动启用或禁用该菜单项。
这里是有关如何启用菜单项的文档。
You are setting the target but have a nil action. Try not setting the target. This may leave it enabled all the time in which case you may have to manually enable or disable the menu item.
Here is the documentation on how menu items get enabled.
我必须重写
validateMenuItem
方法来返回 true
:它实际上并没有使用验证方法,直到我将目标设置为 self,尽管就像您所做的那样
I had to override the
validateMenuItem
method toreturn true
:It didn't actually use the validation method until I set target to self though like you did with
macOS 将根据是否正在处理
enabled
状态自动处理。因此,如果您要设置操作
,您还需要设置操作的目标。和目标:
macOS will handle the
enabled
state automatically based on it is being handled or not. So if you are setting anaction
you also need to set the target of the action.and taget: