mouseDown:在NSTableView中的自定义NSTextField中 [英] mouseDown: in a custom NSTextField inside an NSTableView

查看:825
本文介绍了mouseDown:在NSTableView中的自定义NSTextField中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于视图的 NSTableView 。表格中的每个视图都有自定义文本字段。

I have a view-based NSTableView. Each view in the table has a custom text field.

我想在用户点击表格视图内的文本字段(标签)时触发操作想象一下,在每个表格单元格中有一个具有自定义动作的超链接)。

I'd like to fire an action when the user clicks on the text field (label) inside the table's view (imagine having a hyperlink with a custom action in each table cell).

我创建了一个基本的 NSTextField 捕捉鼠标事件。但是,他们只触发第二次点击,而不是第一次点击。

I've created a basic NSTextField subclass to catch mouse events. However, they only fire on the second click, not the first click.

我尝试使用 NSButton ,并立即启动。

I tried using an NSButton and that fires right away.

以下是自定义标签的代码:

Here's the code for the custom label:

@implementation HyperlinkTextField

- (void)mouseDown:(NSEvent *)theEvent {
    NSLog(@"link mouse down");
}

- (void)mouseUp:(NSEvent *)theEvent {
    NSLog(@"link mouse up");
}

- (BOOL)acceptsFirstResponder {
    return YES;
}

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {
    return YES;
}
@end


推荐答案

结果是 NSTableView NSOultineView 处理 NSTextField 实例与 NSButton 不同。

It turned out that NSTableView and NSOultineView handle the first responder status for NSTextField instances differently than for an NSButton.

获取标签以响应第一次点击的关键像一个按钮是覆盖 [NSResponder validateProposedFirstResponder:forEvent:] 以返回 YES

The key to get the label to respond to the first click like a button is to overwrite [NSResponder validateProposedFirstResponder:forEvent:] to return YES in case of my custom text field class.

文档:

http://developer.apple.com/library/mac/documentation /Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/NSResponder/validateProposedFirstResponder:forEvent

这篇关于mouseDown:在NSTableView中的自定义NSTextField中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆