NSTextField的自动完成方法

Pro*_*ani 3 macos delegates autocomplete objective-c nstextfield

我有complete:NSTextfield的问题触发方法.

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

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

我在这里搜索并发现了一些对我来说毫无意义的例子.

简短问题: 是否有任何方法使用NSTexfields代表之类的controlDidChanged或类似的东西来更容易和清楚地做到这一点?

我只是混淆使用complete:nstextview的方法.

小智 7

我不建议复制整个字符串.这对你的情况很好,但如果你使用自动完成大文本文件,那么你将遇到各种性能和内存问题.您可以跟踪您是否处于更新中.如果您有多个textview,则可以为isCompleting变量创建一个字典.

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

    if (!isCompleting) {
        isCompleting = YES;
        [fieldEditor complete:nil];
        isCompleting = NO;
    }
}
Run Code Online (Sandbox Code Playgroud)