可可 NSPopupButton 问题
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要执行一个操作才能启用 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.