NSTextField自动完成代理方法未调用 [英] NSTextField autocompletion delegate method not called

查看:1158
本文介绍了NSTextField自动完成代理方法未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 NSTextField 实现了以下委托方法以添加自动完成支持:

I implemented the following delegate method for NSTextField to add autocompletion support:

- (NSArray *)control:(NSControl *)control
            textView:(NSTextView *)textView
         completions:(NSArray *)words
 forPartialWordRange:(NSRange)charRange
 indexOfSelectedItem:(NSInteger *)index

问题是这个方法永远不会被调用。我可以验证 NSTextField 委托已正确设置,因为其他委托方法的功能应该如此。

The issue is that this method never gets called. I can verify that the delegate of the NSTextField is set properly because the other delegate methods function as they should.

推荐答案

您需要获取 complete: 在文本字段的字段中调用编辑器。这就是触发完成菜单,但它不会自动调用。如果你没有F5绑定到任何东西,尝试输入您的字段,并打它。完成应该触发; Option-Esc也可以工作。

You'll need to get complete: called on the text field's field editor at some point. That's what triggers the completions menu, but it doesn't get called automatically. If you don't have F5 bound to anything, try typing in your field and hit that. Completion should trigger then; Option-Esc may also work.

如果你想要自动完成 ,需要一些工作。您可以从这样开始:

If you want auto completion, it takes some work. You could start with something like this:

- (void)controlTextDidChange:(NSNotification *)note {
    if( amDoingAutoComplete ){
        return;
    } else {
        amDoingAutoComplete = YES;
        [[[note userInfo] objectForKey:@"NSFieldEditor"] complete:nil];
    }
}

某种标记是必要的,因为触发完成将使 NSControlTextDidChangeNotification 将再次发布,这将导致此被调用,触发完成,更改控制文本,其中...

Some kind of flag is necessary because triggering completion will make NSControlTextDidChangeNotification be posted again, which causes this to be called, triggering completion, which changes the control text, which...

很明显,你需要在某个时候取消设置标志。这将取决于您希望如何处理用户与自动完成的交互 - 是否可能只有一个完成的给定的开始字符串,或者用户需要继续输入以缩小可能性(在这种情况下,您将需要再次触发自动完成?)

Obviously, you'll need to unset the flag at some point. This will depend on how you want to handle the user's interaction with autocompletion -- is there likely to only be one completion for a given start string, or will the user need to keep typing to narrow down possibilities (in which case you'll need to trigger autocompletion again)?

一个简单的标志可能不太适合。看起来虽然通知被重新发布,字段编辑器的 string 将不会改变 - 它只会响应直接键盘输入而改变。在我实现的自动完成,我发现我不得不保留一个最后一个类型字符串的副本,并将每次与字段编辑器的内容进行比较。

A simple flag might not quite do it, either; it seems that although the notification is re-posted, the field editor's string won't have changed -- it will only change in response to direct keyboard input. In my implementation of autocomplete, I found that I had to keep a copy of the "last typed string" and compare that each time to the field editor's contents.

这篇关于NSTextField自动完成代理方法未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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