I wonder if anyone knows why an NSPopover does not respond to mouse events (hover, click) when it is opened from a Button inside an NSMenuItem inside an NSMenu.
And if there’s any solution for this problem?
Here’s a little sample view controller that attaches an NSMenu to its view. The menu has a button that opens the popover on click, and some dummy items that are just there to show that they still receive the mouse events even though the popover is above them:
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMenu *menu = [NSMenu new];
/// Some Dummy Items to show that the popover doesn’t accept the mouse.
NSArray *dummyItems = @[@"item1", @"item2", @"item3"];
for (NSString *itemName in dummyItems) {
NSMenuItem *item = [NSMenuItem new];
item.title = itemName;
item.target = self;
item.action = @selector(doNothing:);
[menu addItem:item];
}
/// The button which presents the popover on click
NSMenuItem *item = [NSMenuItem new];
NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 30)];
button.target = self;
button.action = @selector(showPopover:);
button.title = @"Show Popover";
item.view = button;
[menu addItem:item];
[self.view setMenu:menu];
}
- (void)showPopover:(NSButton *)sender {
/// This popover does not receive the mouse events for some reason.
NSPopover *popover = [NSPopover new];
NSViewController *popoverVC = [NSViewController new];
NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 140, 25)];
button.title = @"I’m not clickable";
button.target = self;
button.action = @selector(showPopover:);
popoverVC.view = button;
[popover setContentViewController:popoverVC];
[popover showRelativeToRect:sender.frame ofView:sender.superview preferredEdge:NSMaxYEdge];
[button.window makeKeyWindow]; /// Does not help
[button.window becomeKeyWindow]; /// Does not help
}
- (void)doNothing:(id)sender {}
@end