NSTextField 的自动补全方法

发布于 2024-11-16 07:35:34 字数 655 浏览 1 评论 0原文

我在触发 NSTextfield 的 complete: 方法时遇到问题。

现在我可以使用 @distinctUnionOfObjects (删除数组重复项的绝佳方法)从文本字段创建一个不同的名称数组,现在我可以使用以下方法发回此文本字段的自动完成功能:
- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView 补全:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index

但是,这种方法不是自动的,我必须在数据输入期间按 ESC 按钮才能弹出文本字段的自动完成建议。

我在这里搜索并发现了一些对我来说没有意义的例子。

简短问题: 是否有任何使用 NSTexfields 委托(如 controlDidChanged 或类似的方法)的方法可以更轻松、更清晰地执行此操作?

我只是对 nstextview 使用 complete: 方法感到困惑。

I'm having problem triggering complete: method for NSTextfield.

for now I can make an distinct array of names from a textfield using @distinctUnionOfObjects ( awesome method to remove duplicates of an array ) and now I can send back autocompletion for this textfield using:
- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index

But, this method is not automatic and I have to press ESC button to pop the autocompletion suggestion up for the textfield during data entry.

I searched here and found some examples that make no sense for me.

Short Question:
Is there any method using NSTexfields delegates like controlDidChanged or something like that to do this more easily and clearly ?

I just confuse using complete: method for nstextview.

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

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

发布评论

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

评论(2

小兔几 2024-11-23 07:35:34

我不建议复制整个字符串。这对于您的情况来说没问题,但如果您对大型文本文件使用自动完成功能,那么您将遇到各种性能和内存问题。您可以跟踪是否正在更新。如果您有多个文本视图,您可以为 isCompleting 变量创建一个字典。

- (void) controlTextDidChange: (NSNotification *)note {
    NSTextView * fieldEditor = [[note userInfo] objectForKey:@"NSFieldEditor"];

    if (!isCompleting) {
        isCompleting = YES;
        [fieldEditor complete:nil];
        isCompleting = NO;
    }
}

I don't recommend copying the whole string. It would be fine for your case, but if you're using autocompletion for a large text file, then you'll have all kinds of performance and memory issues. You can just keep track of whether or not you are in the middle of an update. If you have multiple textviews, you could make a dictionary for the isCompleting variables.

- (void) controlTextDidChange: (NSNotification *)note {
    NSTextView * fieldEditor = [[note userInfo] objectForKey:@"NSFieldEditor"];

    if (!isCompleting) {
        isCompleting = YES;
        [fieldEditor complete:nil];
        isCompleting = NO;
    }
}
小糖芽 2024-11-23 07:35:34

当您的文本字段委托获取 controlTextDidChange: 时,您可以在 字段编辑器。这是当您按 ESC 或 F5 时调用的方法。

- (void) controlTextDidChange: (NSNotification *)note {
    NSTextView * fieldEditor = [[note userInfo] objectForKey:@"NSFieldEditor"];

    [fieldEditor complete:nil];
}

棘手的部分是,当导航完成菜单时,它将导致再次发送 controlTextDidChange: 消息(尽管不更改实际字符串),这将创建无限循环。当您已经处于完成状态时,您将需要某种标志来阻止 complete: 被调用。例如,您可以跟踪用户对字符串所做的最后更改,并将其与字段编辑器的当前值进行比较;如果没有用户发起的更改,则不会导致完成:

BOOL textDidNotChange = [lastTypedString isEqualToString:[fieldEditor string]];

if( textDidNotChange ){
    return;
}
else {
    lastTypedString = [[fieldEditor string] copy];
    [fieldEditor complete];
}

When your text field delegate gets controlTextDidChange:, you can call complete: on the Field Editor. This is the method that gets called when you press ESC or F5.

- (void) controlTextDidChange: (NSNotification *)note {
    NSTextView * fieldEditor = [[note userInfo] objectForKey:@"NSFieldEditor"];

    [fieldEditor complete:nil];
}

The tricky part is that when the completion menu is being navigated, it will cause controlTextDidChange: messages to be sent again, (although without changing the actual string) which will create an infinite loop. You will need some kind of flag to stop complete: from being called when you are already in the middle of a completion. For example, you can keep track of the last change the user made to the string and compare it with the current value of the field editor; if there's no user-initiated change, don't cause completion:

BOOL textDidNotChange = [lastTypedString isEqualToString:[fieldEditor string]];

if( textDidNotChange ){
    return;
}
else {
    lastTypedString = [[fieldEditor string] copy];
    [fieldEditor complete];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文