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

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

问题描述

这个问题似乎很简单,但我尝试了一切我可以想到的,和谷歌多个小时。



我有一个NSSearchField自动完成,基本上复制苹果的< a href =http://developer.apple.com/library/mac/#samplecode/SearchField/Introduction/Intro.html> SearchField示例代码。我在IB中关闭发送完整搜索字符串,因为我不想做搜索,直到用户已经完成他们的文本输入,并且不想做多个搜索(他们是昂贵的)。



当用户键入字段时,当他们按enter键,指定它们接受当前自动完成,我想要NSSearchField的操作。相反,它只是似乎填写自动完成,然后用户必须按下输入第二次动作的触发。基本上,想想开始在Safari中输入一个URL,它的自动完成,然后按enter开始加载页面(触发动作)。



我没有成功的尝试:




  • control:textView:commandSelector :,查找insertNewline :.

  • 覆写controlTextDidEndEditing :.当您按下Enter键完成自动完成时,与上述相同



任何想法?谢谢!

解决方案

我想出了如何使这项工作。





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

   - (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是一个实例变量。这是定义:

  @interface MLFieldEditor:NSTextView 
@end

@implementation MLFieldEditor


- (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)move isFinal:(BOOL)flag
{
//如果用户键入空格,则禁止完成
if(movement == NSRightTextMovement)return;

//显示完全替换
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。



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


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

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).

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:, looking for insertNewline:. It doesn't get fired when they are pressing enter to finish the autocompletion
  • Overriding controlTextDidEndEditing:. Same as above

Any ideas? Thanks!

解决方案

I figured out how to make this work.

You need to override the NSFieldEditor for the NSTextViews.

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

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

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天全站免登陆