Show NSPopover using the NSToolbarItem button

I want to show NSPopover using the NSToolbarItem button on my toolbar.
(i.e. located below the button).

Ideally, I want to pass the NSView button to the button to place it. My question is: how do I get the NSView NSToolbarItem ?

[toolbarbutton view] always returns nil.

+8
cocoa nsview
source share
4 answers

The answer seems to be in the video for the 2011 WWDC session 113, “Full Screen and Aqua Changes.” Basically, put the NSButton inside the NSToolbaritem and use a representation of this.

The blog post is here: http://www.yellowfield.co.uk/blog/?p=33 , and an example project is on github at http://github.com/tevendale/ToolbarPopover

Everything at sprit http://xkcd.com/979 !

+5
source share

You can send the action directly from the NSButton enclosed in NSToolbarItem (this is what you usually should do anyway, consider segmented controls where each segment has its own target / action) and this will do the trick.

+2
source share

Instead of receiving a view from the IBAction sender, connect the IBOutlet directly to the toolbar item and use this to get a relative view:

In your header file:

 @property (weak) IBOutlet NSToolbarItem *theToolbarItem; @property (weak) IBOutlet NSPopover *thePopover; 

In your implementation file, to show popover:

 [self.thePopover showRelativeToRect:[[self.theToolbarItem view] bounds] ofView:[self.theToolbarItem view] preferredEdge:NSMinYEdge]; 

This will also work to display pop-ups from menu items in a toolbar item.

+2
source share

While I made sure Popover was shown using the approach mentioned by Stuart Tevendale, I was having problems when I tried to check (enable / disable) NSToolbarItems with NSToolbarDelegate :

 -(BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem { BOOL enable = YES; NSString *identifier = [toolbarItem itemIdentifier]; // This does never get called because I am using a button inside a custom `NSToolbarItem` if ([identifier isEqualToString:@"Popover"]) { return [self someValidationMechanism]; } // For this the validation works when I am using a standard `NSToolbarItem` else if ([identifier isEqualToString:@"StandardToolbarItem"]){ return [self someOtherValidationMechanism]; } return enable; } 

Therefore, I would advise against showing Popover from NSToolbarItem . An alternative could be a page view: How to show NSPanel as a sheet

0
source share

All Articles