I've seen the other topics dealing with UITextView
delegate, but can't seem to find where my issue come from.
Here is my code, but when I modify my UITextView
, nothing happens and textViewDidBeginEditing
is not called.
import UIKit
var textView1 = UITextView()
class ViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
textView1.delegate = self
textView1 = UITextView(frame: CGRect(x:24,y: 100,width: 340,height: 290))
textView1.textColor = UIColor.black
textView1.backgroundColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.00)
textView1.text = "textView 1"
view.addSubview(textView1)
}
func textViewDidBeginEditing(_ textView: UITextView) {
print("print1")
}
func textViewDidEndEditing(_ textView: UITextView) {
print("print2")
}
}
I've tried with UITextField
, but nothing changed, delegate functions still not called.
Just move your textView1.delegate = self
2 lines down. You are setting delegate for UITextField but in next line you are creating new object without any delegate ;)
You are setting a value from a nil object, you have to Initialize it and then you can set the target delegate.
please change the position of textView1.delegate = self
with next line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With