NSTextField 自动完成委托方法未调用

发布于 2024-10-30 07:29:42 字数 387 浏览 5 评论 0原文

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

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

问题是该方法永远不会被调用。我可以验证 NSTextFielddelegate 设置是否正确,因为其他委托方法按其应有的方式运行。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-11-06 07:29:42

您需要获取 complete: 在文本字段的 字段编辑器 在某个时刻。这就是触发完成菜单的原因,但它不会被自动调用。如果您没有将 F5 绑定到任何内容,请尝试在您的字段中键入并点击它。然后应该触发完成; Option-Esc 也可能有效。

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

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

某种标志是必要的,因为触发完成将使 NSControlTextDidChangeNotification 再次发布,这会导致调用它,触发完成,从而更改控制文本,这.. 显然,

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

一个简单的标志可能也无法完全做到这一点;看起来,虽然通知被重新发布,但字段编辑器的 string 不会改变——它只会响应直接键盘输入而改变。在自动完成的实现中,我发现我必须保留“最后键入的字符串”的副本,并每次将其与字段编辑器的内容进行比较。

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];
    }
}

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文