The documentation for this method says: "Pops up the menu at the specified location." but the pamameters seem to describe a different situations:
item
The menu item to be positioned at the specified location in the view.
location
The location in the view coordinate system to display the menu item.
view
The view to display the menu item over.
The parameters seem to imply that you will only be popping up a MenuItem. What does this method actually do?
I don't know how you get that it might just pop up a menu item — there isn't even such a thing as a pop-up menu item. Nowhere in the parameter descriptions does it say it won't show a pop-up menu. The parameter descriptions all mention the item because they all relate to its positioning. It works as documented.
For convenience, you can set the coordinates of a single menu item. Your menu will be positioned accordingly around that single menu item.
You'll notice that this is how NSPopUpButton behaves: the selected menu item is always positioned directly overtop the button.
If you don't want your menu to behave like that, just pass in your top-most menu item.
You're forgetting something: This is a message you send to the menu object.
In English, the message is:
“Hey menu! Pop up yourself, positioning this item at this location relative to this view.”
The parameters describe where the whole pop-up menu should appear, in terms of positioning a specific item from the menu at a specific location. The whole menu appears as the pop-up, not just the positioning item. Popping up only a single item would be quite useless.
One other thing: This convenient method was introduced in Snow Leopard, so if you're targeting Leopard or earlier, you can't depend on it. You'll have to use another solution, such as the NSPopUpButtonCell I suggested in an answer on one of your earlier questions.
Related
I have a custom subclass of NSTextField. I want to display a context menu on right-click but the menu displays a completely irrelevant Services menu. I understand this is added due to OS settings, but the options are of no use at all in my scenario (e.g. the text fields contain numeric values) and it just adds clutter.
I have overridden textView:menu:forEvent:atIndex: to currently return nil. This means no menu is displayed, which is preferable to the Services menu in my view. However if I try to return an NSMenu instance containing items I actually want, then a Services menu item gets added.
Is there any way for my application or individual text fields to opt out of this?
See allowsContextMenuPlugIns
Indicates whether the pop-up menu allows appending of contextual menu plug-in items.
I have an NSToolbar with NSToolbarItem instances. One of the toolbar buttons is in one of two modes, depending on whether it currently operating (has been clicked) or not. I am handling this in code by changing the icon for the button to have a background rectangle when the command it represents is operational, but I can't help thinking there must be another way.
I've tried using the Selectable checkbox in XCode Interface Builder attribute inspector, and it sort of gives the result I want, except when it is selected I can't click any of the other toolbar items. I also can't see how to deselect it.
I'm a bit of a Cocoa noob so I expect the two state toggle thing is just waiting for me to find it, except so far I haven't been able to.
This seems like it would be a common thing to want to do, thing is how?
I'd like a toolbar button with an attached dropdown menu, like the "Flag" button in the toolbar in Mail.app:
I'd hoped that making a normal NSMenuItem and adding a menu as the menuFormRepresentation would do the trick, but that menu only appears when the button goes into overflow mode.
I had also hoped that adding an NSPopupButton as a custom view would work, but that makes the whole view a menu, whereas I want the left part of the component to behave like a normal button, and the right dropdown part bring up the menu.
Is there some trick to making the NSToolbarItem show a component like this, or is this two custom views stuck together?
There's nothing magical about NSToolbar here. That's just one of the ways you can set up NSSegmentedControl, regardless of whether it appears as a toolbar item's custom view or on its own.
You can't set this up in Interface Builder (storyboard), but NSSegmentedControl has APIs for assigning menus to segments:
segmentControl.setMenu(myMenu, forSegment: 1)
segmentControl.setShowsMenuIndicator(true, forSegment: 1) // for the little arrow
You probably want to set the tracking mode to momentary, since your segment control is acting as a set of visually-connected buttons, not a choose-one-of-N selector.
When the user clicks either segment, your action method will need to use the selectedSegment to decide whether to perform the action associated with the "button" side or ignore the click (letting the menu show for the other side).
I've a cocoa app based on NSDocument, I've two types of document.
Every document type has its own menu items, all items are defined in mainmenu.xib.
As defined in Apple UI guidelines the menu items irrelevant for a doc type are shown grayed.
I have too much menu items so I want to show only relevant items per doc type, another problem is created by the key bindings, for doc type A a particular key binding is associated to a menu item but for doc type B the same key bindind is associated to another menu item but this isn't possible because XCode detects the same key is already used and it removes (correctly) from previous item definition.
I want to create a menu similar to XCode's 'Editor' menu, it shows some items when focus is on a text window and a totally different item set when focus is on a IB file.
Are there best practices? For example splitting menus.
How can I easily create a menu similar to XCode's 'Editor' menu?
Which API cocoa gives us to do it?
You can create the separate menu(s) in the MainMenu.xib and create outlets to them from the App Controller. Then when your document gains focus (see windowDidBecomeMain (reference)), you tell the App controller to switch-out the relevant parts of the main menu.
Switching-out the parts of the main menu is done by trawling the main menu hierarchy and assigning the separate menu at the right place (i.e. [NSMenuItem setSubmenu:] (reference)). You can make this easier for yourself by setting tags, using Interface Builder, in those menu items and then using [NSMenu itemWithTag:] (reference) to find them programmatically.
I am making my own NSMenu programmatically and popping it up when a button is pressed.
I create NSMenuItems and add them with the NSMenu method insertItem:atIndex:.
For some reason whatever item is at position zero in the menu does not show up. Is this how the menu is supposed to work?
Yes, this is how the menu is supposed to work, if the NSPopUpButton pullsDown. The first item corresponds to the title of the button; just insert "" or whatever you want. It won't show up.
So you're building your menu in reverse order (by iteratively calling insertItem:anItem atIndex:0)? Why not just build it from the top down and successively call addItem:? I've done this lots and never had issues with items disappearing.