Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
667 views
in Technique[技术] by (71.8m points)

objective c - How to make NSSearchField send action upon autocompletion?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...