NSMenu click in OS X - objective-c

How can I detect when the menu in menu bar is clicked? I mean when whatever is clicked like for example "file" menu not a concrete position in the menu.
I tried to connect IBActions from menu items in interface builder from my code, but it does not work from the main menu item like "File" only from the submenus within this menu

First of all, what are you trying to achieve in the big picture? Perhaps knowing when the menu bar has been clicked is not the best approach.
You might be able to achieve what you're interested in by observing the NSMenuDidBeginTrackingNotification notification, but it's hard to tell. For example, this will fire for keyboard-initiated tracking, too.

Go to MainMenu.xib and look at the menu layout. It will be something like
Main Menu
Menu Item - File
Menu - File
Right click on The NSMenu you want to observe, not the NSMenuItem. attach the delegate to an object or controller of your choosing.
Then implement
- (void)menuWillOpen:(NSMenu *)menu

Related

WatchKit context menu, how to tell which row it was for?

I have a WKInterfaceTable in my interface controller and have added a Context Menu to the controller. On tap holding a specific row, the row depresses and displays the context menu. How do I know what row the menu is for?
The context menu is for the entire view, it has no concept of where you tapped.
The following gets called on your WKInterfaceController.
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex

Adding options to NSPopUpButton in Interface Builder

So when I add an NSPopUpButton in Xcode's Interface builder, it automatically has 3 options in the dropdown which can be worked with in the IB; however, I want to have 8 options in my popup. I know I can just add these in code, but it would be much more convenient if there was a way to add them right in the IB, without making the popup button a property. Is there any way to do this?
If you expand the NSPopupButton, there is a "Popup Button Cell". Expand that and you will find a "Menu". You can drag new NSMenuItems to that menu in Interface Builder.

Contextual menu not displayed in 10.5 for application build in 10.7

I have created an application in 10.7, where i used contextual menu for tableview. When right click event occurs, contextual menu is displayed. This works fine.
But, when the same executable is run in 10.5, and right clicked on table view contextual menu is not displayed at all.
It seems menuNeedsUpdate: method is not called at all.
Can any one suggests for this issue?
Regards,
iSIght
In Leopard you have to subclass NSTableView, implement menuForEvent: and fetch and return the menu from your table's delegate or datasource.
Alternatively you could try hooking up the table's menu outlet in Interface Builder.

use UIMenuController with text

So i would like to use the UIMenuController to display text. i would like to add in help buttons (press a little button and something will pop up describing what something does) that would display either a UIMenuController or something that looks similar to it. it would just display a few lines of text and dismiss when pressed.
it kinda looks like UIMenuController is only meant for button selection. i could just use one button and set the title to the message i wish to display, but am doubtful on how well that would work. are there any other options available?
A UIMenuController takes it content from its menuItems property and the documentation says :
The default value is nil (no custom menu items). Each menu item is an
instance of the UIMenuItem class. You may create your own menu items,
each with its own title and action selector, and add them to the
editing menu through this property. Custom items appear in the menu
after any system menu items.
So you are obliged to use UIMenuItem objects as content for your UIMenuController. UIMenuItem only inherits from NSObject, and stores only a title and an action, both required. This make you unable to use any other kind of data as UIMenuItem.
As it is not an UIView, you can give it a UIButton or a UIImageView.
You could think to override UIMenuController but again it is a direct child of NSObject, so it doesn't have any behavior for customization.
The only solution left to you, is to rewrite your own UIMenuController, deriving it from UIToolbar for example. This would give you more or less the same look, you just have to customize the arrow and the round corner.

Putting an NSPopUpButton in an NSToolbar

Having wrestled with NSPopUpButton in a previous question here I am now trying to place a NSPopUpButton inside an NSToolbar. Essentially I want to create something similar to what XCode has by default on the left hand side of it's toolbar. E.g. a pop up button with an action button next to it.
I have seen a method that show's a programmatic way of creating an NSPopUpButton and then adding it to an NSToolbar, but then I can't work out how to do all the Binding stuff that was so handy last time.
Interface Builder hasn't been very helpful, so any help gratefully received.
P.S. Could I solve this by creating a custom view (containing an NSPopUpButton with the usual bindings) and then adding the custom view to the toolbar?
It's actually pretty easy to do what you want here. In Interface Builder, switch to the tree view (the second button on the View Mode segmented control). Expand the window and the toolbar. Then, from the library, drag a popup button onto the toolbar. Interface Builder will embed a new popup button in a custom view for you automatically.
To actually put the button on the toolbar, double click on the toolbar in the window. This will bring up the customization sheet. You can drag the popup button to the desired location on the toolbar.
If you wanted to do this programmatically, you would create a custom view containing your popup button. Then, you'd need to assign it to a new outlet so you can refer to it programmatically. In the toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar method, you would create a new NSToolbarItem per usual, and call setView: to assign the custom view to the toolbar item.
Old post, but note you can also double-click the toolbar in the window and drag straight into the "Allowed Toolbar Items".
You will want to open up the toolbar view anyway so you can drag things in or out of the default toolbar items.