如何在UITextViewwith中检测数据的变化Swift?以下代码不进行任何检测。
UITextView
Swift
我在声明UITextView:
@IBOutlet weak var bodyText: UITextView! optional func textViewDidChange(_ textView: UITextView!) { println(bodyText.text) }
谢谢斯科特
您需要设置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 } }