NSTableView 中 NSTextFieldCell 的自定义字段编辑器
我有一个自定义的 NSTableView 子类,其中填充了几个自定义的 NSTextFieldCell 子类。我希望能够使用箭头键更改编辑的单元格。
我可以通过创建自定义字段编辑器(通过子类化 NSTextView
)并从窗口委托返回它来实现此目的,如下所示:
- (id) windowWillReturnFieldEditor:(NSWindow *) aWindow toObject:(id) anObject {
if ([anObject isEqual:myCustomTable]) {
if (!myCustomFieldEditor) {
myCustomFieldEditor = [[MyNSTextViewSubclass alloc] init];
[myCustomFieldEditor setTable:anObject];
}
return myCustomFieldEditor;
}
else {
return nil;
}
}
在 MyNSTextViewSubclass
中,我覆盖 moveUp:
、moveDown:
、moveLeft:
和 moveRight:
方法来实现我想要的功能,并且一切正常。唯一的问题是字段编辑器的行为不再像文本字段单元格编辑器。例如,当我按 Enter 键时,它会在文本字段中插入换行符,而不是结束编辑。
如何创建一个自定义字段编辑器,其响应与 NSTextFieldCell
的默认编辑器完全相同(除了我将覆盖的那四个函数)?或者是否有更好的如何更改 moveUp:
、moveDown:
、moveLeft:
和 moveRight:
的功能?
编辑:当选择文本字段进行编辑时,字段编辑器似乎将文本字段设置为其委托。在这种情况下,按照 此处,但是当我在我的 中实现该功能时NSTextFieldCell
子类或我的 NSTableView
子类,它永远不会被调用。为什么不呢?
I have a custom NSTableView
subclass filled with several custom NSTextFieldCell
subclasses. I would like to be able to change the edited cell by using the arrow keys.
I am able to accomplish this by creating a custom field editor (by subclassing NSTextView
) and returning it from the window delegate like so:
- (id) windowWillReturnFieldEditor:(NSWindow *) aWindow toObject:(id) anObject {
if ([anObject isEqual:myCustomTable]) {
if (!myCustomFieldEditor) {
myCustomFieldEditor = [[MyNSTextViewSubclass alloc] init];
[myCustomFieldEditor setTable:anObject];
}
return myCustomFieldEditor;
}
else {
return nil;
}
}
In MyNSTextViewSubclass
, I override the moveUp:
, moveDown:
, moveLeft:
, and moveRight:
methods to implement my desired functionality, and that all works fine. The only problem is that the field editor no longer behaves like a text field cell editor. For example, when I hit the Enter key, it inserts a newline into the text field instead of ending the editing.
How do I create a custom field editor that responds exactly like the default one does for an NSTextFieldCell
(except for those four functions that I will override)? Or is there a better way to change the functionality ofmoveUp:
, moveDown:
, moveLeft:
, and moveRight:
?
EDIT: It appears that the field editor sets the text field as its delegate when it is selected for editing. In that case, it might be helpful to just attach to the control:textView:doCommandBySelector:
delegate method as described here, but when I implement that function in either my NSTextFieldCell
subclass or my NSTableView
subclass, it never gets called. Why not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我几乎花了一整天的时间来解决这个问题,但我终于弄清楚了。为了能够使用箭头键遍历我的 NSTableView 子类,我必须将以下方法添加到我的 NSTableView 中:
这是因为默认字段编辑器是 NSTextView (不是 NSControl),所以我需要使用
协议。设置为其委托的视图是 NSTableView,不是 NSTextFieldCell。 moveSelectionTo... 函数是在我的 NSTableView 子类中定义的自定义函数,用于跟踪当前编辑的单元格,然后相应地移动它。I spent almost all day on this problem, but I finally figured it out. In order to be able to traverse my NSTableView subclass with the arrow keys, I had to add the following method to my NSTableView:
This is because the default field editor is an NSTextView (not an NSControl) so I needed to use the
<NSTextViewDelegate>
protocol. The view that is set as its delegate is the NSTableView, not the NSTextFieldCell. ThemoveSelectionTo...
functions are custom functions defined in my NSTableView subclass that keep track of the currently edited cell and then move it around accordingly.Apple 文档中可能相关的条目:
setFieldEditor:
控制共享接收器布局管理器的文本视图是否充当字段编辑器。
参数
flag:
YES
使共享接收器布局管理器的文本视图充当字段编辑器,否则NO
。讨论
字段编辑器将 Tab、Shift-Tab 和 Return (Enter) 解释为结束编辑并可能更改第一响应者的提示。非字段编辑器接受这些字符作为文本输入。有关字段编辑器的更多信息,请参阅“文本字段、文本视图和字段编辑器”。默认情况下,文本视图不充当字段编辑器。
Possibly related entry in Apple documentation:
setFieldEditor:
Controls whether the text views sharing the receiver’s layout manager behave as field editors.
Parameters
flag:
YES
to cause the text views sharing the receiver's layout manager to behave as field editors,NO
otherwise.Discussion
Field editors interpret Tab, Shift-Tab, and Return (Enter) as cues to end editing and possibly to change the first responder. Non-field editors instead accept these characters as text input. See “Text Fields, Text Views, and the Field Editor” for more information on field editors. By default, text views don’t behave as field editors.
这个问题的更一般标题的答案可以在这个答案中找到:https://stackoverflow.com/a/8865953/43615基本上
,一个子类
NSTextFieldCell
并覆盖fieldEditorForView:
,其中只需创建一个NSTextView
的自定义子类并设置其fieldEditor
属性设置为YES
。The answer to the more general title of this question can be found in this answer: https://stackoverflow.com/a/8865953/43615
Basically, one subclasses
NSTextFieldCell
and overridesfieldEditorForView:
, where one simply creates a custom subclass ofNSTextView
and sets itsfieldEditor
property toYES
.完成您所需的最简单方法是在表视图的委托中实现
control:textView:doCommandBySelector:
。另请参阅我对类似问题的回答:带有 NSTableView 的箭头键
The easiest way to accomplish what you need is to implement
control:textView:doCommandBySelector:
in the table view's delegate.See also my answer to a similar question here: Arrow keys with NSTableView
这些应该在子类
NSTextFieldCell
对象的keyDown:(NSEvent *)event
方法中重写。您检查按下的键(箭头之一),否则调用super
。These should be overridden in
keyDown:(NSEvent *)event
method of your subclassedNSTextFieldCell
object. You check the pressed key (one of the arrows) and otherwise call up tosuper
.