菜单未更新的NSSearchField(NSSearchField with menu not being updated)

编程入门 行业动态 更新时间:2024-10-15 14:20:12
菜单未更新的NSSearchField(NSSearchField with menu not being updated)

我有一个基于文档的应用程序,在文档视图中,我有一个NSSearchField。 在搜索字段中,我启用了菜单,我可以显示该菜单,并且我已将相关操作与菜单项相关联。 其中一个菜单项称为“匹配大小写”。 我希望能够在此菜单项旁边放置(并删除)支票。 当我尝试这样做时,菜单不显示检查。

-(IBAction)searchMenuMatchCase:(id)sender { NSMenuItem *smi = [searchMenu itemWithTitle:@"Match Case"]; if (searchCaseSensitive) { searchCaseSensitive = false; [[searchMenu itemWithTitle:@"Match Case"] setState:NSOffState]; } else { searchCaseSensitive = true; [[searchMenu itemWithTitle:@"Match Case"] setState:NSOnState]; } [searchMenu update]; NSLog(@"SM State %ld",[smi state]); }

代码被执行,我得到一条日志消息,显示状态从0到1到0到1.但是菜单项旁边从不检查。 当我在调试时查看菜单项对象时,我确实在切换之前看到“state”值设置为0和1。

关于我缺少什么的任何建议?

I haev a Document Based Application, and In the document view, I have a NSSearchField. In the search field, I have enabled the menu, which I get to show up, and I have associated actions with the menu item. One of the menu items is called "Match Case". I want to be able to put (and remove) a check next this menu item. When I attempt to do so, the menu does not show the check.

-(IBAction)searchMenuMatchCase:(id)sender { NSMenuItem *smi = [searchMenu itemWithTitle:@"Match Case"]; if (searchCaseSensitive) { searchCaseSensitive = false; [[searchMenu itemWithTitle:@"Match Case"] setState:NSOffState]; } else { searchCaseSensitive = true; [[searchMenu itemWithTitle:@"Match Case"] setState:NSOnState]; } [searchMenu update]; NSLog(@"SM State %ld",[smi state]); }

The code gets executed, and I get a log message showing the state going from 0 to 1 to 0 to 1. But there is never a check next to the menu item. When I look at the menu item object while debugging, I do see the "state" value set to 0 and 1 before I toggle it.

Any suggestions as to what I am missing?

最满意答案

为了解决这个问题,我不得不使用NSMenuDelegate并使用validateMenuItem方法。 这样在绘制菜单之前调用了validateMenuItem方法,我可以确保在绘制菜单之前正确设置了状态。 我相信发生的事情是我在原始代码中获得的菜单项不是正在绘制的实际菜单,而只是一个模板。

/** This is called when the user selectes the "Match Case" menu item found in the Search field */ -(IBAction)searchMenuMatchCase:(id)sender { if (searchCaseSensitive) { searchCaseSensitive = false; } else { searchCaseSensitive = true; } } /** This is called Each time a menu is shown. */ -(BOOL) validateMenuItem:(NSMenuItem *)menuItem { if ([[menuItem title] compare:@"Match Case"] == 0) { if (searchCaseSensitive) { [menuItem setState:NSOnState]; } else { [menuItem setState:NSOffState]; } } return YES; }

To get around this issue, I had to use the NSMenuDelegate and use the method validateMenuItem. This way the validateMenuItem method was called before the menu was drawn, and I could make sure the state was set correctly before the menu was drawn. I believe what was happening was that the menu item I was getting in the original code was not the actual menu that was being drawn, but just a template.

/** This is called when the user selectes the "Match Case" menu item found in the Search field */ -(IBAction)searchMenuMatchCase:(id)sender { if (searchCaseSensitive) { searchCaseSensitive = false; } else { searchCaseSensitive = true; } } /** This is called Each time a menu is shown. */ -(BOOL) validateMenuItem:(NSMenuItem *)menuItem { if ([[menuItem title] compare:@"Match Case"] == 0) { if (searchCaseSensitive) { [menuItem setState:NSOnState]; } else { [menuItem setState:NSOffState]; } } return YES; }

更多推荐

本文发布于:2023-08-06 20:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:菜单   NSSearchField   updated   menu

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!