NSMenuItem 启用项目

发布于 2024-12-25 07:23:08 字数 1243 浏览 1 评论 0原文

我有一个 NSMenuItem,里面有一堆项目,但是......该列表没有启用。

我的意思是:
List

这是我的代码:

- (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:
List

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2025-01-01 07:23:08

当您创建 NSMenuItem 时,您的项目必须具有有效的目标和有效的选择器。这意味着目标不能为零,并且必须响应传递的选择器。请记住,在这种情况下,NULL 选择器将不会启用菜单项。

NSMenu *myMenu;
NSMenuItem *myItem;

myMenu = [[NSMenu alloc] initWithTitle:@""];
myItem = [[NSMenuItem alloc] initWithTitle:@"Test" action:@selector(validSelector:) keyEquivalent:@""];
[myItem setTarget:myTarget];
[myMenu addItem:myItem];
// Do anything you like
[myMenu release];
[myItem release];

编辑:我看到您在创建菜单项后使用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.

NSMenu *myMenu;
NSMenuItem *myItem;

myMenu = [[NSMenu alloc] initWithTitle:@""];
myItem = [[NSMenuItem alloc] initWithTitle:@"Test" action:@selector(validSelector:) keyEquivalent:@""];
[myItem setTarget:myTarget];
[myMenu addItem:myItem];
// Do anything you like
[myMenu release];
[myItem release];

EDIT: I saw you're calling -[NSMenuItem setEnabled:] with YESafter 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文