Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to control the NSMenuItem item that is initially highlighted when opening a menu?

I'm working on the details of a symbols pop up button, similar to what Xcode 3 at the top of its editor window.

My controller object is the delegate of the NSMenu that is shown when the NSPopUpButton is shown. I implement the two methods numberOfItemsInMenu: and menu:updateItem:atIndex:shouldCancel: to populate the menu right before it's about to be displayed.

However, the title and image of the selected NSMenuItem have to change each time the user changes the selection in the editor NSTextView, or makes changes to the text; just as is the case with Xcode.

The problem I'm having is when the user goes to click on the NSPopUpButton to display the NSMenu, the selected NSMenuItem and the item that should be selected do not match up, since the menu doesn't have the proper number of items yet.

I'm wondering if there is a way to control which NSMenuItem is initially highlighted and tracked when the user clicks to open the menu. As it stands, the first item it always highlighted and tracked or, if the user had previously selected a item, that item is highlighted and tracked.

I tried explaining as best I could, but here is a image illustrating my problem:

http://imgur.com/izGvh

I want the highlighted item to be the same as the selected item when the user opens the menu. Any ideas?

like image 534
willbur1984 Avatar asked Apr 23 '11 05:04

willbur1984


2 Answers

I would use a NSPopupbutton - it can do what you want. maybe you even hide it?

like image 111
Daij-Djan Avatar answered Sep 28 '22 16:09

Daij-Djan


I am not very sure that I understood your problem but If we can add tags to these menu Items. e.g.

[mMenuItemOne setTag : 1];
[mMenuItemTwo setTag : 2];
[mMenuItemThree setTag : 3];
[mMenuItemFour setTag : 4];}

we can select any Menu item using [_popUp selectItemWithTag: _selectedItem];

like image 37
Vinay Verma Avatar answered Sep 28 '22 16:09

Vinay Verma