如何让 NSSearchField 在自动完成时发送动作? [英] How to make NSSearchField send action upon autocompletion?

查看:31
本文介绍了如何让 NSSearchField 在自动完成时发送动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题看起来很简单,但我已经尝试了所有我能想到的方法,并在谷歌上搜索了几个小时.

This question seems straightforward but I've tried everything I can think of, and Googled for hours.

我有一个可以自动完成的 NSSearchField,基本上是复制 Apple 的 SearchField 示例代码.我在 IB 中关闭了发送整个搜索字符串",因为我不想在用户完成文本输入之前进行搜索,并且不想进行多次搜索(它们很昂贵).

I have an NSSearchField that does autocomplete, basically copying Apple's SearchField sample code. I have turned off "Sends Whole Search String" in IB because I don't want to do the search until the user has finalized their text entry, and don't want to do multiple searches (they are expensive).

当用户在字段中输入时,当他们按下 Enter 键并指定他们接受当前的自动完成时,我希望 NSSearchField 的操作被触发.相反,它似乎只是填写了自动完成功能,然后用户必须再次按 Enter 才能触发该操作.基本上,想想开始在 Safari 中输入 URL,它会自动完成,然后按 Enter 开始加载页面(触发操作).他们无需再次按 Enter 键即可开始加载页面.

As the user types in the field, when they press enter, specifying that they accept the current autocompletion, I want the action for the NSSearchField to fire. Instead, it just seems to fill-in the autocompletion, then the user has to press enter a second time for the action to fire. Basically, think of starting to type in a URL in Safari, it autocompletes, and pressing enter starts loading the page (firing the action). They don't need to press enter a second time to start loading the page.

我尝试过但没有成功的事情:

Things I've tried without success:

  • control:textView:commandSelector:,寻找insertNewline:.当他们按 Enter 完成自动完成时,它不会被触发
  • 覆盖 controlTextDidEndEditing:.同上

有什么想法吗?谢谢!

推荐答案

我想出了如何实现这一目标.

I figured out how to make this work.

您需要覆盖 NSTextViews 的 NSFieldEditor.

You need to override the NSFieldEditor for the NSTextViews.

为了提供一个覆盖版本,在 NSWindow 的委托中:

To provide an overridden version, in the NSWindow's delegate:

- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)client
{
    if ([client isKindOfClass:[NSSearchField class]])
    {
        if (!_mlFieldEditor)
        {
            _mlFieldEditor = [[MLFieldEditor alloc] init];
            [_mlFieldEditor setFieldEditor:YES];
        }
        return _mlFieldEditor;
    }
    return nil;
}

_mlFieldEditor 是一个实例变量.定义如下:

_mlFieldEditor is an instance variable. Here is the definition:

@interface MLFieldEditor : NSTextView
@end

@implementation MLFieldEditor


-  (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag
{
    // suppress completion if user types a space
    if (movement == NSRightTextMovement) return;

    // show full replacements
    if (charRange.location != 0) {
        charRange.length += charRange.location;
        charRange.location = 0;
    }

    [super insertCompletion:word forPartialWordRange:charRange movement:movement isFinal:flag];

    if (movement == NSReturnTextMovement)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:MLSearchFieldAutocompleted object:self userInfo:nil];
    }
}

@end

关键部分是 [super insertCompletion...] 之后的 NSReturnTextMovement.

The key part is the NSReturnTextMovement after the [super insertCompletion...].

第一部分将更改它,以便键入空格键不会执行自动完成,这是我所做的评论:如何防止 NSSearchField 覆盖输入的字符串使用第一个自动完成列表条目?

The first part will change it so that typing the space key won't perform autocompletion, which was a comment I had made on: How to prevent NSSearchField from overwriting entered strings using the first autocompletion list entry?

这篇关于如何让 NSSearchField 在自动完成时发送动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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