可可 NSPopupButton 问题

发布于 2024-10-11 16:16:46 字数 853 浏览 9 评论 0原文

在我的 Cocoa 应用程序中,在一个视图中,我通过界面生成器添加了一个 NSPopupButton 并将其相应地与源文件链接, 现在在代码中,我正在动态创建菜单并使用 NSPopupbutton 添加菜单, 这是我在 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];

当我运行应用程序时,最初按钮是启用的,但是当我单击箭头时,包括菜单和按钮被禁用

请告诉我我做错了什么。

In my Cocoa Application, In One view i added a NSPopupButton via Interface builder and linked that accordingly with the source file,
Now in the Code, i am creating Menu dynamically and adding menu with the NSPopupbutton,
This is i have done it in WindowDidLoad
Please refer the code below

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

When i run the application, then initially the button is Enabled, but when i click the arrow , including Menu and button gets disabled

Please tell me what i am doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2024-10-18 16:16:46

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

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

You need an action for the NSPopUpButton to be enabled. Target is what's optional (as nil means first responder).

You do not need to set a target/action for the menu items, since the popup can tell you which item was selected when it calls its action against its target.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文