本文介绍了如何使NSControl(例如NSTokenField)忽略鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体地说,我想使NSTokenField忽略鼠标事件,因为我在NSTableCellView中使用它只是为了以标记化的方式显示数据,而不允许进行任何编辑.

Specifically, I'd like to make an NSTokenField ignore mouse events because I'm using it in a NSTableCellView just to display data in a tokenized way without allowing any editing.

设置令牌字段的enabled = NO是有效的,除了它使令牌变灰并使其难以阅读文本.

Setting the token field's enabled = NO works, except that it greys out the tokens and makes it hard to read the text.

设置令牌字段的editable = NO非常接近我想要的设置-防止在保留令牌字段外观的同时进行编辑-除非将鼠标悬停在令牌上,否则它们会亮起.如果我只能阻止这种情况,那我就会做生意.

Setting the token field's editable = NO is pretty close to what I want—it prevents editing while preserving the token field's look—except that when I mouse over the tokens, they light up. If I could just prevent that, I'd be in business.

我怀疑我需要继承某些东西并重写某些NSResponder方法,但不确定该怎么做.我尝试对NSTokenField进行子类化,并覆盖了mouseEntered:mouseMoved:却什么也不做,但是那也不起作用.

I suspect I need to subclass something and override some NSResponder methods, but not quite sure what to do. I tried subclassing NSTokenField and overriding mouseEntered: and mouseMoved: to do nothing, but that didn't work either.

推荐答案

尝试了很多东西之后,我终于根据IuliusCæsar的建议使它起作用.

After trying a lot of stuff, I finally got this to work based on Iulius Cæsar's suggestion.

诀窍是继承NSTextField的子类并覆盖trackingAreas:

The trick was to subclass NSTextField and override trackingAreas:

- (NSArray *)trackingAreas
{
    return [NSArray array];
}

创建字段时仅删除字段的跟踪区域还不够,因为该字段处于滚动视图中,有时会重新创建跟踪区域.

Simply deleting the field's tracking areas when creating it wasn't quite enough, because the field was in a scroll view and sometimes the tracking areas would be re-created.

这篇关于如何使NSControl(例如NSTokenField)忽略鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:43