我该如何使我的NSTextField在应用程序启动时不突出显示其文本? [英] How can I make my NSTextField NOT highlight its text when the application starts?

查看:63
本文介绍了我该如何使我的NSTextField在应用程序启动时不突出显示其文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动我的应用程序时,第一个NSTextField被选择为:

When my application launches, the first NSTextField is being selected like this:

我可以很好地编辑NSTextField,但是当我按Enter键结束编辑时,文本再次变为选中状态,并且编辑没有结束.

I can edit the NSTextField fine, but when I press enter to end the editing, the text becomes selected again, and the editing does not end.

我遵循了Apple教程此处,并且在永久突出显示文本字段时,我遇到了同样的问题.

I followed the Apple tutorial here, and I had the same problem with the text field being perpetually highlighted.

如何停止此操作?我希望文本字段不是应用程序的第一响应者,因此不会立即对其进行编辑,并且在对其进行编辑时,在文本字段之外单击将结束它.在后一种情况下,我不确定将[[textField window]makeFirstResponder:nil]放在何处以停止编辑.

How do I stop this? I would like it so the text field is not the first responder of the app so it's not edited right away, and when it is being edited, clicking outside of the text field will end it. I'm not sure where to put the [[textField window]makeFirstResponder:nil] to stop the editing in the latter case.

我正在运行优胜美地10.10.2.

I'm running Yosemite 10.10.2.

推荐答案

由于NSTextFieldbecomeFirstResponder的默认实现,您的文本字段正在选择文本.

Your text field is selecting the text, due to the default implementation of becomeFirstResponder in NSTextField.

为防止选择,请子类NSTextField,并覆盖becomeFirstResponder以取消选择任何文本:

To prevent selection, subclass NSTextField, and override becomeFirstResponder to deselect any text:

- (BOOL) becomeFirstResponder
{
    BOOL responderStatus = [super becomeFirstResponder];

    NSRange selectionRange = [[self  currentEditor] selectedRange];
    [[self currentEditor] setSelectedRange:NSMakeRange(selectionRange.length,0)];

    return responderStatus;
}

结果是,该字段在获得焦点时没有选择文本.

The resulting behavior is that the field does not select the text when it gets the focus.

要使第一响应者什么都不做,请在应用程序启动完成后调用makeFirstResponder:nil.我喜欢将NSObject子类化以定义doInitWithContentView:(NSView *)contentView,然后从我的NSApplicationDelegate调用它.在下面的代码中,_windowIBOutlet:

To make nothing the first responder, call makeFirstResponder:nil after your application finishes launching. I like to subclass NSObject to define doInitWithContentView:(NSView *)contentView, and call it from my NSApplicationDelegate. In the code below, _window is an IBOutlet:

- (void) applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application

    [_menuController doInitWithContentView:_window.contentView];
}

应用程序启动时您的字段获得焦点的原因是因为该窗口自动将焦点赋予了第一个控件.它通过从左到右,自上而下扫描来确定首先要考虑的内容(它首先从左到右扫描,因为放置在右上角的文本字段仍会聚焦). 一个警告是,如果窗口为restorable,并且您从Xcode终止了应用程序,则最后聚焦的字段将保留上次执行的聚焦状态.

The reason your field is getting focus when the application starts is because the window automatically gives focus to the first control. It determines what is considered first, by scanning left to right, top down (it scans left to right first, since a text field placed at the top right will still get focused). One caveat is that if the window is restorable, and you terminate the application from within Xcode, then whatever field was last focused will retain the focus state from the last execution.

这篇关于我该如何使我的NSTextField在应用程序启动时不突出显示其文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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