斯威夫特& NSTextField:如何使用文本完成 [英] Swift & NSTextField: How to work with Text Completion

查看:177
本文介绍了斯威夫特& NSTextField:如何使用文本完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 NSTextField 创建这样的自动完成列表:

I want to create autocomplete list like this for NSTextField:

我发现了这一点:$ b​​ $ b https ://developer.apple.com/documentation/appkit/nscontroltexteditingdelegate/1428925-control

I have found this: https://developer.apple.com/documentation/appkit/nscontroltexteditingdelegate/1428925-control

optional func control(_ control: NSControl, 
             textView: NSTextView, 
          completions words: [String], 
  forPartialWordRange charRange: NSRange, 
  indexOfSelectedItem index: UnsafeMutablePointer<Int>) -> [String]

有人可以解释在任何示例中如何使用它吗?
我不太了解。

Can somebody please explain how to use this on any example? I can't really understand.

我尝试实现此功能,但没有任何效果。您可以在下面找到我的代码。

I tried to implement this, but nothing works. You can find my code below.

谢谢您

class ViewController: NSViewController, NSTextFieldDelegate {

@IBOutlet weak var InputField: NSTextField!

override func viewDidLoad() {
    super.viewDidLoad()

    InputField.delegate = self
}

func control(_ control: NSControl, textView: NSTextField, completions words: [String], forPartialWordRange charRange: NSRange, indexOfSelectedItem index: UnsafeMutablePointer<Int>) -> [String] {

    let words = ["Hello", "Brother"]

    return words
}

@IBAction func CompleteButton(_ sender: NSButton) {
    print("pressed")
    InputField.complete(nil)

}
}

但是,如果我尝试按下按钮,则会在控制台中出现此错误:

But if I try to press the button, I get this error in my console:

pressed
[NSTextField complete:]: unrecognized selector sent to instance 0x10066ae00
[General] -[NSTextField complete:]: unrecognized selector sent to instance 0x10066ae00


推荐答案

NSTextField 没有实现 complete: NSTextView 却没有实现。 NSTextField 使用字段编辑器 NSTextView 来编辑其内容,请参见文本字段,文本视图,以及字段编辑器。使用 currentEditor()从文本字段获取字段编辑器。

NSTextField doesn't implement complete:, NSTextView does. NSTextField uses a NSTextView, the field editor, to edit its contents, see Text Fields, Text Views, and the Field Editor. Use currentEditor() to get the field editor from the text field.

@IBAction func completeButton(_ sender: NSButton) {
    inputField.currentEditor()?.complete(nil)
}

这篇关于斯威夫特&amp; NSTextField:如何使用文本完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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