我有一个瞬态NSPopover,当它打开时,我点击父窗口中的一个按钮,然后取消弹出窗口并点击“吞下”。只有第二次按下按钮才能正确触发操作。
有没有办法直接将第一次点击传递给控件并一步关闭popover?
答案 0 :(得分:1)
NSPopover确实似乎吞下了目标位置视图的事件。其他观点都很好。我的解决方案是让代表将最后一个鼠标按下事件转发到目标视图,如果命中测试显示它是被点击的视图。不幸的是,当委托收到消息时,NSApp -currentEvent为零 - 不知道为什么。所以我向应用代表添加了一个事件监视器,如下所示:
- (void)addEventMonitor
{
if (self.eventMonitor) {
return;
}
self.eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:(NSLeftMouseDownMask) handler:^(NSEvent *incomingEvent) {
NSEvent *result = incomingEvent;
self.monitoredEvent = result;
return result;
}];
}
当委托关闭时,它会检查目标中最后一个受监控事件是否为
- (void)popoverDidClose:(NSNotification *)notification
{
// [NSApp currentEvent] is nil here
NSEvent *event = [(BPApplicationDelegate *)[NSApp delegate] monitoredEvent];
if (event && (event.type & NSLeftMouseDown)) {
NSPoint pt = [self.targetView.superview convertPoint:event.locationInWindow fromView:nil];
if ([self.TargetView hitTest:pt]) {
[NSApp postEvent:event atStart:NO];
}
}
}