2011-01-14 38 views
0

在我的Cocoa應用程序,在一個視圖中我通過界面生成的代碼添加一個NSPopupButton和鏈接是相應地與源文件, 現在,我動態地創建菜單,並與NSPopupbutton添加菜單, 這是我在WindowDidLoad 做了,請參考下面可可NSPopupButton問題

NSString *pThemeName; 
for(;index<count;index++) 
{ 
    pThemeName = [pThemeArray objectAtIndex:index]; 

    /* If its valid them go ahead and add that into the 
    list 

    */ 
    if([CommFileManager IsValidThemeName:pThemeName]){ 
    menuItem = [[NSMenuItem alloc] initWithTitle:pThemeName action:@selector(selectThemeName) keyEquivalent:@""]; 
    [menuItem setTarget:self]; 
    [pPopUpmenu addItem:menuItem]; 
    [menuItem setTag:index]; 
    [menuItem release]; 
    } 
} 
[pPopupButton setTarget:self]; 
[pPopupButton setMenu:pPopUpmenu]; 
[pPopupButton selectItem:[pPopUpmenu itemAtIndex:5]]; 
[pPopUpmenu release]; 

當我運行該應用程序,然後開始按鈕被啓用的代碼,但是當我點擊箭頭,包括菜單和按鈕被禁用

請告訴我我是doi恩錯了。

+0

雖然你在彈出按鈕上調用`setTarget:`而沒有設置動作,它看起來很好。由於您要將操作添加到每個菜單項中,因此不要指定按鈕本身的目標/操作。 – d11wtq 2011-01-14 08:32:04

+0

嗨,我試過沒有,也沒有運氣:( – Amitg2k12 2011-01-14 08:53:37

回答

0

您需要啓用NSPopUpButton的操作。 Target是可選的(因爲nil表示第一響應者)。

您不需要爲菜單項設置目標/操作,因爲彈出窗口可以告訴您在針對其目標調用其操作時選擇了哪個項目。