UItextView delegate swift - textViewDidBeginEditing not called
14,514
Solution 1
You are setting a value from a nil object, you have to Initialize it and then you can set the target delegate.
Solution 2
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 ;)
Solution 3
please change the position of textView1.delegate = self
with next line.
Author by
VH24
Updated on June 04, 2022Comments
-
VH24 almost 2 years
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 myUITextView
, nothing happens andtextViewDidBeginEditing
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. -
VH24 almost 7 yearsDid not change anything :/
-
Gabs almost 7 yearsMaybe because you are initializing twice.
-
VH24 almost 7 yearsWhere have I a 2nd initialisation ? I tried to only keep let textView1 = UITextView(frame: CGRect(x:24,y: 100,width: 340,height: 290)) But it's the same.
-
Gabs almost 7 yearsI suggest you this: textView1.frame = CGRect(x:24,y: 100,width: 340,height: 290) textView1.delegate = self 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)
-
VH24 almost 7 yearsoh then it's coming from me.. maybe because I've the 8.2 version.
-
Gabs almost 7 yearsI don't think so @VH24
-
VH24 almost 7 yearsI change my code with yours then run the app. I change the text of textView1 but there is nothing printed in the output area.. It's surely something easy but I'm really stuck with this issue..
-
rmaddy almost 7 years"Please try" is not a useful description. Clearly explain what was wrong and how your answer solves the issue.
-
rmaddy almost 7 years1) Post actual code, not a picture. Pictures can't be referenced or searched. 2) How does this answer improve on the other, previous answers that already stated that the
textView1.delegate = self
line needs to be moved? -
VH24 almost 7 yearsProblem solved after restarting my computer and changing the position of textView1.delegate = self with second next line. There was also probably an issue with xcode like a bug, solved after the restart. thanks !