gpt4 book ai didi

swift - 使用 NSTextViewDelegate 和 textViewDidChange 不工作来监听 NSTextView 的变化

转载 作者:搜寻专家 更新时间:2023-11-01 05:48:49 29 4
gpt4 key购买 nike

我希望每次用户对我的 NSTextView 进行更改时触发一个函数。我让它在 iOS 应用程序中工作,现在正试图让它在 OS X 应用程序中工作。我为我的 NSTextView 创建了一个导出并编写了以下 Swift 代码:

import Cocoa

class ViewController: NSViewController, NSTextViewDelegate {

@IBOutlet var textViewOutlet: NSTextView!

func textViewDidChange(textView: NSTextView) {
print("Text view changed!")
}
}

我没有收到任何错误,但我的声明没有打印出来。

最佳答案

应该是textDidChange(notification:)。像这样尝试:

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

关于swift - 使用 NSTextViewDelegate 和 textViewDidChange 不工作来监听 NSTextView 的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36342666/

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