使用 NSMenuItems 从 NSArray 填充 NSMenu - 需要替代建议

发布于 2024-12-01 00:05:38 字数 2667 浏览 2 评论 0原文

所以我有以下代码:

- (void)addSupportLinksMenuItems
{
    NSString *subMenuTitle;
    NSString *getURL;
    if (!supportLinks) {
        supportLinks = [[NSArray alloc] initWithArray:[settings objectForKey:@"supportLinks"]];

    }
    for(NSDictionary *object in supportLinks){
        // A couple of Keys in the Dict inside the Array
        subMenuTitle = [object objectForKey:@"subMenuTitle"];
        getURL = [object objectForKey:@"getURL"];
        NSInteger n = [ supportLinks indexOfObject:object];
        NSInteger menuTag = n +255;
        //[ supportLinkItem setImag
        supportLinkArrayItem = [supportLinkItem
                                  insertItemWithTitle:subMenuTitle
                                  action:@selector(openSupportLink:)
                                  keyEquivalent:@""
                                  atIndex:n];

        // Set a menu tag to programatically update in the future
        [ supportLinkArrayItem setTag:menuTag];
        [ supportLinkArrayItem setToolTip:getURL];
        [ supportLinkArrayItem setTarget:self];

    }

    //supportLinkItem
}

这从 NSArray 动态生成子菜单项,并允许我根据所选的选择(在特定浏览器中)打开 url:

-(IBAction)openSupportLink:(id)sender
{
    NSLog(@"Was passed Menu: %@",sender);
    NSInteger menuTag = [sender tag];
    NSInteger n = menuTag - 255;
    NSString *getURL = [[supportLinks objectAtIndex:n] objectForKey:@"getURL"];
    [self openPageInSafari:getURL];
}

- (void)openPageInSafari:(NSString *)url
{
    NSDictionary* errorDict;
    NSAppleEventDescriptor* returnDescriptor = NULL;
    NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:
                                   [NSString stringWithFormat:
                                   @"\
                                   tell app \"Safari\"\n\
                                   activate \n\
                                   make new document at end of documents\n\
                                   set URL of document 1 to \"%@\"\n\
                                   end tell\n\
                                   ",url]];
    returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
    [scriptObject release];

}

我的问题是,虽然这看起来效果很好,但我会喜欢为 NSMenu supportLinkItem 设置图像,这是我的 .h 文件的样子:

IBOutlet NSMenu *supportLinkItem;
NSMenuItem *supportLinkArrayItem;

并且插座链接到子菜单项,因为我已将其(父项?-术语?)创建为 NSmenu,但它没有请允许我作为 - (void)setImage:(NSImage *)menuImage 方法访问它,因为它不是 NSMenuitem。现在我想也许我只是在这里做了一些奇怪的事情,从技术上讲,当你将“子菜单项”拖到界面生成器中时,它是一个 NSMenuItem 而不是 NSMenu,我的代码再次完美地工作,除了我无法设置菜单的图像,我认为这是不行的,但也许有类似的方法可以从 NSArray 中读取数据来填充一组子菜单。

So I have the following code:

- (void)addSupportLinksMenuItems
{
    NSString *subMenuTitle;
    NSString *getURL;
    if (!supportLinks) {
        supportLinks = [[NSArray alloc] initWithArray:[settings objectForKey:@"supportLinks"]];

    }
    for(NSDictionary *object in supportLinks){
        // A couple of Keys in the Dict inside the Array
        subMenuTitle = [object objectForKey:@"subMenuTitle"];
        getURL = [object objectForKey:@"getURL"];
        NSInteger n = [ supportLinks indexOfObject:object];
        NSInteger menuTag = n +255;
        //[ supportLinkItem setImag
        supportLinkArrayItem = [supportLinkItem
                                  insertItemWithTitle:subMenuTitle
                                  action:@selector(openSupportLink:)
                                  keyEquivalent:@""
                                  atIndex:n];

        // Set a menu tag to programatically update in the future
        [ supportLinkArrayItem setTag:menuTag];
        [ supportLinkArrayItem setToolTip:getURL];
        [ supportLinkArrayItem setTarget:self];

    }

    //supportLinkItem
}

This dynamically generates an submenu items from an NSArray and allows me to open the url based on the choice that was selected (in a specific browser):

-(IBAction)openSupportLink:(id)sender
{
    NSLog(@"Was passed Menu: %@",sender);
    NSInteger menuTag = [sender tag];
    NSInteger n = menuTag - 255;
    NSString *getURL = [[supportLinks objectAtIndex:n] objectForKey:@"getURL"];
    [self openPageInSafari:getURL];
}

- (void)openPageInSafari:(NSString *)url
{
    NSDictionary* errorDict;
    NSAppleEventDescriptor* returnDescriptor = NULL;
    NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:
                                   [NSString stringWithFormat:
                                   @"\
                                   tell app \"Safari\"\n\
                                   activate \n\
                                   make new document at end of documents\n\
                                   set URL of document 1 to \"%@\"\n\
                                   end tell\n\
                                   ",url]];
    returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
    [scriptObject release];

}

My question is, while this seems to work great , I would like to set an image for the NSMenu supportLinkItem, here is what my .h file looks like:

IBOutlet NSMenu *supportLinkItem;
NSMenuItem *supportLinkArrayItem;

And the outlet is linked to the sub menu item, as I have created its (parent? -terminology?) as a NSmenu, it does not allow me to access this as the - (void)setImage:(NSImage *)menuImage method as its not a NSMenuitem. Now I think maybe I have just done something weird here as , technically when you drag the "Sub Menu Item" into interface builder its a NSMenuItem not a NSMenu, again my code works flawlessly except for my inability to set the image of the menu, Which I think is a no go but perhaps there is similar way to read from an NSArray to populate a set of sub menus.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

白云不回头 2024-12-08 00:05:38

我能够通过更新 nib 文件中的图像来解决此问题,因为 nib 认为它是 nsmenuitem。

在此处输入图像描述

I was able to resolve this by updating the image in the nib file as the nib thinks its a nsmenuitem.

enter image description here

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