2011-08-24 60 views
0

所以我有以下代碼:填充NSMenu與NSMenuItems - 需要其他建議

- (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的子​​菜單項,並允許我根據選擇打開網址被選中(在特定的瀏覽器):

-(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; 

而出口鏈接到子菜單項,因爲我已經創建它的(父? -terminology?)作爲NSmenu,它不允許我將它作爲 - (void)setImage:(NSImage *)menuImage方法訪問,因爲它不是NSMenuitem。現在我想也許我剛剛在這裏做了一些奇怪的事情,從技術上講,當您將「子菜單項」拖到界面構建器中時,其NSMenuItem不是NSMenu,再次,我的代碼完美無瑕地工作,除了無法設置菜單圖像外,我認爲這是一個不行,但也許有類似的方式來讀取NSArray來填充一組子菜單。

回答

0

我能夠通過更新nib文件中的圖像來解決此問題,因爲nib認爲它是nsmenuitem。

enter image description here