【问题标题】:Populate a NSMenu from an NSArray with NSMenuItems - Need Alternative Suggestions使用 NSMenuItems 从 NSArray 填充 NSMenu - 需要替代建议
【发布时间】:2011-11-02 23:44:57
【问题描述】:

所以我有以下代码:

- (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 中读取以填充一组子菜单。

【问题讨论】:

    标签: objective-c cocoa nsarray nsmenuitem nsmenu


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 2022-11-11
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多