【发布时间】:2019-11-24 00:23:46
【问题描述】:
我发现了这个: 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]
有人可以解释如何在任何示例中使用它吗? 实在看不懂。
我试图实现这一点,但没有任何效果。你可以在下面找到我的代码。
提前谢谢你
我的代码:
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)
} }
但如果我尝试按下按钮,我会在控制台中收到此错误:
pressed
[NSTextField complete:]: unrecognized selector sent to instance 0x10066ae00
[General] -[NSTextField complete:]: unrecognized selector sent to instance 0x10066ae00
【问题讨论】:
-
你了解代表吗?
-
@Willeke 我是这方面的初学者 :) 刚刚添加了我的代码,也许有帮助。
标签: swift delegates nstextfield