小编典典

UITextView数据更改迅速

swift

如何在UITextViewwith中检测数据的变化Swift?以下代码不进行任何检测。

我在声明UITextView

@IBOutlet weak var bodyText: UITextView!

optional func textViewDidChange(_ textView: UITextView!) {
    println(bodyText.text)
}

谢谢斯科特


阅读 240

收藏
2020-07-07

共1个答案

小编典典

您需要设置UITextView
委托并在其中实现textViewDidChange:方法。不幸的是,我不知道是否可以在线获得快速文档。所有链接都转到Objective-
C文档。

该代码将如下所示:( 已针对SWIFT 4.2更新

class ViewController: UIViewController, UITextViewDelegate { //If your class is not conforms to the UITextViewDelegate protocol you will not be able to set it as delegate to UITextView

    @IBOutlet weak var bodyText: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        bodyText.delegate = self //Without setting the delegate you won't be able to track UITextView events
    }

    func textViewDidChange(_ textView: UITextView) { //Handle the text changes here
        print(textView.text); //the textView parameter is the textView where text was changed
    }
}
2020-07-07