更改NSTableView中的“输入”键行为 [英] Change 'enter' key behaviour in NSTableView

查看:91
本文介绍了更改NSTableView中的“输入”键行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Mac(而非iPhone)创建一个简单的数据输入应用程序。
它有一个NSTableView(位于NSScrollView内),其中包含一个NSTextField,用户可以逐行将数据键入其中。

I'm trying to create a simple data entry application for the Mac (not iPhone). It has an NSTableView (inside an NSScrollView) which contains an NSTextField into which the user keys data, line by line.

到目前为止,我的工作正常好的,除了为了能够在文本字段中键入任何内容外,您必须先按Enter键。然后,当您最后按 Enter键时,该字段将退出编辑模式,但突出显示仅位于同一行。在输入的每一行数据之间必须按 Enter,向下箭头, Enter不是很有效。

What I have so far is working okay, except that in order to actually be able to type anything into the text field, you have to press the 'Enter' key first. Then when you press the 'Enter' key at the end, the field leaves 'edit' mode, but the highlight just sits on the same row. Having to press 'Enter', 'down arrow', 'Enter' between each row of data entered is not very efficient.

我更希望的是,当您按下 Enter键时,光标会向下移动到下一行,并且该字段会自动进入编辑模式。

What I would prefer is that when you press the 'Enter' key, the cursor moves down to the next row and the field is automatically placed into 'edit' mode.

我本以为这样的事情应该很简单,但是我整晚都在梳头,没有任何进展,我什至不知道哪一个我应该去的方式。

I had thought that such a thing should be fairly straightforward, but I have been tearing my hair out all evening without any progress, and I still don't even know which way I should be heading.

我尝试在Xcode的检查器中查找某些属性设置,但是什么也看不到。

I tried looking for some attribute settings in the 'inspectors' in Xcode, but couldn't see anything.

在网上搜索时,使用急救员的建议似乎总是会以某种方式重新出现。为此,我只能设法使蓝色突出显示环按需向下移动到TableView周围,但这似乎仍无法将文本字段置于编辑模式或移至另一行。

Searching online, suggestions seemed to keep coming back to somehow using First Responder. Playing around with this, I've only managed to get the blue highlight ring to move down around my TableView on demand, but this still doesn't seem to put the text field into 'edit' mode nor move to another line.

最后,我找到了一些有关子类化和实现自己的文本字段的信息,但这似乎是一种漫长而费解的方法。

Finally I found some information about subclassing and implementing your own textfields, but this seems like a very long and convoluted way to achieve this.

有人可以为我指出正确的方法吗?

Can someone point me in the right direction as to how to do this?

我可能使用了错误的控制方式吗?也许还有其他更合适的选择。请注意,每一行都是不同的,所以我不希望使用自动换行等。

Am I maybe using the wrong kind of control? Perhaps something else is more appropriate. Note that each line is distinct, so I don't want things like word wrap etc.

推荐答案

使用第一响应程序,但重要的是不要由窗口的字段编辑器(NSTextView的实例)来处理文本编辑,该窗口在文本编辑开始时成为第一响应程序。

You were on the right track with the First Responder, but it's important to not that text editing is handled by the window's Field Editor (an instance of NSTextView), which becomes the first responder when text editing begins. The field editor informs its client (in this case your Table View) during editing.

处理此问题的最简单方法是将NSTableView子类化并覆盖其textDidEndEditing:方法。像这样的东西应该让你开始:

The easiest way to handle this would be to subclass NSTableView and override its textDidEndEditing: method. Something like this should get you started:

- (void) textDidEndEditing:(NSNotification *)notification {
    [super textDidEndEditing:notification];

    int textMovement = [[notification.userInfo valueForKey:@"NSTextMovement"] intValue];
    if (NSReturnTextMovement == textMovement) {

        NSText *fieldEditor = notification.object;

        // The row and column for the cell that just ended editing
        NSInteger row = [self rowAtPoint:fieldEditor.frame.origin];
        NSInteger col = [self columnAtPoint:fieldEditor.frame.origin];       

        if (++col >= self.numberOfColumns) {
            col = 0;
            if (++row >= self.numberOfRows) return;
        }

        [self selectRowIndexes:[NSIndexSet indexSetWithIndex:row] 
            byExtendingSelection:NO];
        [self editColumn:col row:row withEvent:nil select:YES];
    }

}

这将在表的列和行中从左到右,从上到下进行。尽管您说过您正在使用NSTextField,但我想您是说您在基于单元格的表中使用NSTextFieldCell。这是另一个重要的区别。如果您不熟悉字段编辑器的概念,建议您阅读

This will progress left-to-right, top-to-bottom through the table's columns and rows. Though you said you were using a NSTextField, I presume you meant you were using a NSTextFieldCell in a cell-based table. That's another important distinction. If you're not familiar with the concept of the field editor, I suggest you read up.

这篇关于更改NSTableView中的“输入”键行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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