开发者

Populate a NSMenu from an NSArray with NSMenuItems - Need Alternative Suggestions

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 supportL开发者_StackOverflow中文版inkItem, 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.


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

Populate a NSMenu from an NSArray with NSMenuItems - Need Alternative Suggestions

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜