gpt4 book ai didi

swift - NSTextView textDidChange 事件

转载 作者:搜寻专家 更新时间:2023-10-30 23:00:26 27 4
gpt4 key购买 nike

我正在做类似的事情:

@IBOutlet var textView: NSTextView!

override func viewDidLoad() {
super.viewDidLoad()

textView.delegate = self
}

func textDidChange(notification: NSNotification) {

}

我确实在最开始添加了“NSTextViewDelegate”。

如果“textView”中的文本发生变化,我想设置标签的字符串值。

问题是我的程序会崩溃而没有任何错误消息(断点在“fun textDidChange()......”)。即使 func textDidChange 为空,它也会崩溃。

我做错了什么吗?

谢谢

最佳答案

使用这个,Swift 3

class ViewController: NSViewController, NSTextViewDelegate {

@IBOutlet var textView: NSTextView!

override func viewDidLoad() {
super.viewDidLoad()

textView.delegate = self

// NSTextViewDelegate conforms to NSTextDelegate
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
print(textView.string)
}
}

记住

  • textDidChange(_ notification: Notification)中有一个_
  • Swift 3 更喜欢 Notification 而不是 NSNotification
  • 设置委托(delegate) textView.delegate = self

关于swift - NSTextView textDidChange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28294478/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com