无论如何使(包装)NSTextField在按下返回键时写回车符?

Sea*_*ean 3 cocoa objective-c nstextfield

我想使用一个包装文本字段,它可能在我的应用程序中包含回车符.有没有办法强制NSTextField对象将回车写入文本区域,而不是在按下Return键时将其动作发送到目标?

小智 10

这是覆盖在技术Q&A QA1454,这也列举原因之一会使用NSTextField,而不是NSTextView在这种情况下.

您可以在文本字段委托中实现以下方法:

- (BOOL)control:(NSControl*)control
    textView:(NSTextView*)textView
    doCommandBySelector:(SEL)commandSelector
{
    BOOL result = NO;

    if (commandSelector == @selector(insertNewline:))
    {
        // new line action:
        // always insert a line-break character and don’t cause the receiver
        // to end editing
        [textView insertNewlineIgnoringFieldEditor:self]; 
        result = YES;
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)