Objective c 可可豆钮扣问题

Objective c 可可豆钮扣问题,objective-c,cocoa,macos,xib,nsviewcontroller,Objective C,Cocoa,Macos,Xib,Nsviewcontroller,在我的Cocoa应用程序中,在一个视图中,我通过Interface builder添加了一个NSPopupButton,并相应地将其与源文件相链接, 现在在代码中,我正在动态创建菜单,并使用NSPOUPPBUTON添加菜单, 这是我在WindowDidLoad中完成的 请参考下面的代码 NSString *pThemeName; for(;index<count;index++) { pThemeName = [pThemeArray objectAtIndex:index];

在我的Cocoa应用程序中,在一个视图中,我通过Interface builder添加了一个NSPopupButton,并相应地将其与源文件相链接, 现在在代码中,我正在动态创建菜单,并使用NSPOUPPBUTON添加菜单, 这是我在WindowDidLoad中完成的 请参考下面的代码

 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];
当我运行应用程序时,最初按钮是启用的,但当我单击箭头时,包括菜单和按钮都被禁用


请告诉我我做错了什么

您需要一个操作才能启用NSPopUpButton。目标是可选的,因为nil表示第一响应者


您不需要为菜单项设置目标/操作,因为当弹出窗口针对其目标调用其操作时,它可以告诉您选择了哪个项。

在我看来,这很好,尽管您在没有设置操作的情况下在弹出按钮上调用setTarget:。由于要向每个菜单项添加操作,只需将按钮本身的目标/操作保留为未指定即可。嗨,我也尝试过,但没有成功: