NSTextView中的NSTextFieldCell的自定义字段编辑器 [英] Custom field editor for NSTextFieldCell in an NSTableView

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

问题描述

我有一个定制的 NSTableView 子类,填充了几个自定义的 NSTextFieldCell 子类。我想通过使用箭头键来更改编辑的单元格。

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.

我可以通过创建自定义字段编辑器(通过子类化 NSTextView ),并从窗口委托中返回它:

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

MyNSTextViewSubclass ,我覆盖 moveUp: moveDown: moveLeft: code>和 moveRight:方法来实现我所需的功能,并且一切正常。唯一的问题是字段编辑器不再像文本字段单元格编辑器那样工作。例如,当我按Enter键时,它会在文本字段中插入一个换行符,而不是结束编辑。

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.

如何创建自定义字段编辑器对于 NSTextFieldCell (除了那些我将覆盖的四个函数除外),还有一个更好的方法来更改功能的 moveUp: moveDown: moveLeft:,以及 moveRight:

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:?

编辑:看起来,字段编辑器设置文本字段作为其代理选择进行编辑时。在这种情况下,只要附加到控件:textView:doCommandBySelector: delegate方法可能会有帮助,如这里,但是当我实现这个功能在我的 NSTextFieldCell 子类或我的 NSTableView 子类中,它永远不会被调用。为什么不呢?

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?

推荐答案

我几乎全天都在这个问题上,但我终于明白了。为了能够使用箭头键遍历NSTableView子类,我不得不在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:

- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector {
    if(aSelector == @selector(moveUp:)) {
        [self moveSelectionToPreviousRow];
        return YES;
    } else if(aSelector == @selector(moveDown:)) {
        [self moveSelectionToNextRow];
        return YES;
    } else if(aSelector == @selector(moveLeft:)) {
        [self moveSelectionToPreviousColumn];
        return YES;
    } else if(aSelector == @selector(moveRight:)) {
        [self moveSelectionToNextColumn];
        return YES;
    }
    return NO;
}

这是因为默认字段编辑器是NSTextView(而不是NSControl)我需要使用< NSTextViewDelegate> 协议。设置为其委托的视图是NSTableView, NSTextFieldCell。 moveSelectionTo ... 函数是在我的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. The moveSelectionTo... functions are custom functions defined in my NSTableView subclass that keep track of the currently edited cell and then move it around accordingly.

这篇关于NSTextView中的NSTextFieldCell的自定义字段编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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