gpt4 book ai didi

multithreading - Cocoa:延迟一个方法调用,直到处理后续调用

转载 作者:行者123 更新时间:2023-12-03 17:21:40 26 4
gpt4 key购买 nike

我发现自己处于以下情况:我有一个带有事件单元格的 NSTableView 子类。当我单击用户界面上的其他位置时,委托(delegate)方法 (i) 被触发,这又会触发 (ii) (我自己的方法),然后 cocoa 继续处理单击,导致最后两次调用。我对这个序列感到惊讶和失望,因为我假设鼠标单击将是第一个而不是最后一个处理的事件。这也给我带来了一个问题,因为 manageState 的理想实现依赖于我在 mouseDown: 中所做的一些处理,但是当然当 manageState > 调用了 mouseDown: 尚未执行。

他们是延迟执行manageState直到mouseDown返回的方法吗?例如,在 manageState 中,我会说类似 Stop!鼠标按下事件可能在事件队列中。等到完成,然后继续。正如前一句所暗示的,该方法也可能由鼠标按下以外的其他事件触发。在这种情况下,无需留意鼠标按下事件,处理可以正常继续。

MOUSE CLICK on NSTextView while NSTableView cell has focus...

+-------------------------+--------------------------------------------------------+
|Event |Triggered because |
+=========================+========================================================+
|controlTextDidEndEditing:|The mouse click ends the editing session |
| |of the active cell in my table view |
+-------------------------+--------------------------------------------------------+
|manageState |This method is a selector that belongs to a notification|
| |that is fired from within controlTextDidEndEditing: |
+-------------------------+--------------------------------------------------------+
|becomeFirstResponder |I clicked on the NSTextView instance |
+-------------------------+--------------------------------------------------------+
|mouseDown: |Finally, the click that started it all is processed |
+-------------------------+--------------------------------------------------------+

最佳答案

这是我自己想出来的。我担心我必须拼凑出一个基于线程的解决方案,但更多的文档搜索发现了 NSEvent 类方法 addLocalMonitorForEventsMatchingMask:handler

当我的应用程序启动时,我调用此方法,定义关联的 block ,并“告诉”该方法我想要查找哪些事件。然后,每当检测到这些事件之一时,该 block 就会运行。至关重要的是,这发生在事件引发的任何处理启动之前。在 block 中,您有机会停止事件的运行,或让处理继续,您还可以访问事件本身。回到我的问题,这意味着我实际上在表的顶部插入了一行。这是完美的,因为它允许我在任何后续事件执行之前对该事件采取行动。

[NSEvent addLocalMonitorForEventsMatchingMask:NSLeftMouseDownMask 
handler:^NSEvent *(NSEvent *event) {
// whenever an event of type NSLeftMouseDownMask is detected, this code
// will run before any other event processing

// do some processing, then...

return event; // or nil, if you want to block the event
}];

关于multithreading - Cocoa:延迟一个方法调用,直到处理后续调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23939949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com