objective-c - Transient NSPopover 燕子先点击父窗口控件

标签 objective-c swift cocoa

我有一个 transient NSPopover,当它打开时,我单击父窗口中的一个按钮,弹出窗口被关闭,单击“吞下”。只有第二次单击按钮才能正确触发操作。

有没有办法将第一次点击直接传递给控件并一步关闭弹出窗口?

最佳答案

NSPopover 似乎确实吞下了目标位置 View 的事件。其他观点还好。我的解决方案是让委托(delegate)将最后一个鼠标按下事件转发到目标 View ,如果 HitTest 显示它是被点击的 View 。不幸的是,当委托(delegate)收到消息时,NSApp -currentEvent 为 nil - 不知道为什么。所以我像这样向应用程序委托(delegate)添加了一个事件监视器:

- (void)addEventMonitor 
{ 
    if (self.eventMonitor) {
        return;
    }

    self.eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:(NSLeftMouseDownMask) handler:^(NSEvent *incomingEvent) {
    NSEvent *result = incomingEvent;
    self.monitoredEvent = result;
    return result;
    }];

当委托(delegate)关闭时,它会检查最后一个监视事件是否在目标中:

- (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];
        }
    }
}

关于objective-c - Transient NSPopover 燕子先点击父窗口控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32026411/

相关文章:

ios - 为什么使用照片框架的示例应用程序在每次更改后都使用 stopCachingImagesForAllAssets?

ios - 动态框架dyld : Symbol not found:

cocoa - 使用CALayer的renderInContext : method with geometryFlipped

iphone - NSMutableArray 问题 - iPhone

objective-c - 循环遍历类似名称的控件

swift - awakeFromNimb 不会在 TableView reloadData Swift 上被调用

iphone - 不再应用 xcode 应用程序图标

objective-c - 包含一个主要可执行文件和一个辅助应用程序的应用程序包

ios - UITextView 类别继承无法在 iOS7 设备上按预期工作

objective-c - MacOS - 按最近使用的优先顺序获取正在运行的应用程序