How can you detect a change of data in a UITextView
with Swift
? The following code does not do any detection.
I am declaring the UITextView
:
@IBOutlet weak var bodyText: UITextView!
optional func textViewDidChange(_ textView: UITextView!) {
println(bodyText.text)
}
Thanks Scott
You need to set UITextView delegate and implement textViewDidChange: method in it. Unfortunately, I do not know if swift documentation is available online. All the links go to the objective-c documentation.
The code will look like this: (updated for 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
}
}