Show NSPopover from NSToolbarItem Button

后端 未结 4 983
孤城傲影
孤城傲影 2021-02-08 12:50

I want to show an NSPopover from an NSToolbarItem button in my toolbar.
(i.e. positioned below the button).

Ideally, I want to pass the

相关标签:
4条回答
  • 2021-02-08 13:18

    While I did achieve that the Popover was shown using the approach mentioned by Stuart Tevendale, I did run into problems when I tried to validate (enable / disable) the NSToolbarItems using the 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;
    }
    

    So I would advise not to display a Popover from NSToolbarItem. An alternative might be to show a Page Sheet: How to show a NSPanel as a sheet

    0 讨论(0)
  • 2021-02-08 13:20

    Instead of getting the view from the IBAction sender, connect an IBOutlet directly to the toolbar item and use that to get the relative view:

    In your header file:

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

    In your implementation file, to show the popover:

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

    This will also work for showing popups from menu item selections inside a toolbar item.

    0 讨论(0)
  • 2021-02-08 13:41

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

    0 讨论(0)
  • 2021-02-08 13:42

    The answer appears to be in the video for the 2011 WWDC Session 113, "Full Screen and Aqua Changes." Basically, put an NSButton inside the NSToolbaritem and use the view of that.

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

    All in the sprit of http://xkcd.com/979!

    0 讨论(0)
提交回复
热议问题