Show NSPopover from NSToolbarItem Button

后端 未结 4 984
孤城傲影
孤城傲影 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

提交回复
热议问题