NSMenuItem 启用项目
我有一个 NSMenuItem,里面有一堆项目,但是......该列表没有启用。
我的意思是:
这是我的代码:
- (void)didFetchNewList:(NSArray *)list
{
NSArray *smallList = [list subarrayWithRange:NSMakeRange(0, 10)];
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
for (NSDictionary *dict in smallList)
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd @ HH:mm:ss"];
NSMenuItem *soMenuItem = [[NSMenuItem alloc] initWithTitle:
[dateFormatter stringFromDate:[dict objectForKey:@"date"]]
action:nil
keyEquivalent:@""];
[soMenuItem setEnabled:YES];
[menu addItem:soMenuItem];
}
[menu addItem:[NSMenuItem separatorItem]];
NSMenuItem *soMenuItem = [[NSMenuItem alloc]
initWithTitle:@"Settings" action:nil keyEquivalent:@"S"];
[soMenuItem setEnabled:YES];
[menu addItem:soMenuItem];
[statusItem setMenu:menu];
[statusItem setEnabled:YES];
}
我将所有内容设置为启用,但它仍然处于禁用状态。我该如何解决这个问题?
I have an NSMenuItem with a bunch of items in it, however... the list just doesn't get enabled.
What I mean:
This is my code:
- (void)didFetchNewList:(NSArray *)list
{
NSArray *smallList = [list subarrayWithRange:NSMakeRange(0, 10)];
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
for (NSDictionary *dict in smallList)
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd @ HH:mm:ss"];
NSMenuItem *soMenuItem = [[NSMenuItem alloc] initWithTitle:
[dateFormatter stringFromDate:[dict objectForKey:@"date"]]
action:nil
keyEquivalent:@""];
[soMenuItem setEnabled:YES];
[menu addItem:soMenuItem];
}
[menu addItem:[NSMenuItem separatorItem]];
NSMenuItem *soMenuItem = [[NSMenuItem alloc]
initWithTitle:@"Settings" action:nil keyEquivalent:@"S"];
[soMenuItem setEnabled:YES];
[menu addItem:soMenuItem];
[statusItem setMenu:menu];
[statusItem setEnabled:YES];
}
I am setting everything as enabled, yet it is still disabled. How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您创建
NSMenuItem
时,您的项目必须具有有效的目标和有效的选择器。这意味着目标不能为零,并且必须响应传递的选择器。请记住,在这种情况下,NULL 选择器将不会启用菜单项。编辑:我看到您在创建菜单项后使用
YES
调用-[NSMenuItem setEnabled:]
。这不是必需的,因为它们将默认启用。编辑2:正如 NSGod 指出的(参见下面的评论),目标可以为零。在这种情况下,应用程序的第一响应者将收到传递的方法。也就是说,只要第一响应者实施了该方法。 (编辑 3) 但是,如果不是这种情况,该方法将被发送到响应者链中的下一个响应者。这将持续下去,直到找到响应选择器的响应者,或者没有剩余的响应者可供检查。当没有找到响应者时,您的菜单项将不会被启用。
When you create an
NSMenuItem
your item will have to have a valid target and a valid selector. This means the target cannot be nil, and has to respond to the passed selector. Keep in mind that in this case a NULL selector will not enable the menu item.EDIT: I saw you're calling
-[NSMenuItem setEnabled:]
withYES
after you create the menu item. This not necessary, as they will be enabled by default.EDIT 2: As NSGod pointed out (see comment below) the target can be nil. In that case the first responder of your application will receive the passed method. That is, as long as the first responder has that method implemented. (edit 3) However if this is not the case, the method will be sent to the next responder in the responder chain. This continues until either a responder is found that responds to the selector or there are no responders left to examine. When no responder is found you menu item won't get enabled.