1

I was looking at Text View Placeholder Swift. And it seems to me that I did everything that this post has told me to do. However, it is not working. More specifically, the textViewDidBeginEditing() and textViewDidEndEditing() functions are not executing. And the reason why I think this is because I wrote the println("hello") and println("YO") but it is not printing anything to the console. Anybody have a solution to fix this problem?

Here is my code:

import UIKit

class WritePrayerRequestViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var writePrayerRequestTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        writePrayerRequestTextView.becomeFirstResponder()
        writePrayerRequestTextView.layer.borderWidth = 0.5
        writePrayerRequestTextView.layer.borderColor = UIColor.lightGrayColor().CGColor
        writePrayerRequestTextView.layer.cornerRadius = 7
        writePrayerRequestTextView.textColor = UIColor.lightGrayColor()
        writePrayerRequestTextView.text = "Write your prayer request here."
    }

    func textViewDidBeginEditing(writePrayerRequestTextView: UITextView) {
        println("hello")
        if writePrayerRequestTextView.textColor == UIColor.lightGrayColor() {
            writePrayerRequestTextView.text = nil
            writePrayerRequestTextView.textColor = UIColor.blackColor()
        }
    }

    func textViewDidEndEditing(textView: UITextView) {
        println("YO")
        if textView.text.isEmpty {
            textView.text = "Write your prayer request here."
            textView.textColor = UIColor.lightGrayColor()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
Community
  • 1
  • 1
Jae Kim
  • 625
  • 3
  • 12
  • 23

1 Answers1

1

are you sure the UITextView's delegate is assigned correctly? eg writePrayerRequestTextView.delegate = self in the viewDidLoad

Fonix
  • 11,447
  • 3
  • 45
  • 74
  • Yes. That works! thank you so much! One thing that always confused me was the whole delegate thing. Could you please explain to me what that is? In other words, what is the purpose of `UITextViewDelegate` and setting the `delegate = self`? If you prefer, we can go to a chat room? – Jae Kim Sep 10 '15 at 04:21
  • Another thing is though, even after I set `delegate = self`, the second function, `textViewDidEndEditing` is not being executed. Any reason why that might be? – Jae Kim Sep 10 '15 at 04:23
  • 1
    i think i can sum it up here, so a delegate is basically telling a class that a certain set of functionality needs to be handled by another class, so putting `UITextViewDelegate` in the class declaration is telling your project "hey if you have a UITextView, i implement these functions so can do work for you" so thats not enough to make a UITextView use those functions, you need to set a particular instance of a UITextView to make your class its delegate (because there could be multiple classes that are delegates, so you have to tell it which one it is exactly) so hence my answer – Fonix Sep 10 '15 at 04:40
  • 1
    `textViewDidEndEditing` only gets called when the first responder is resigned i think, aka when the textview loses focus and the keyboard hides, might have to implement the function `- textViewShouldEndEditing:` and return true – Fonix Sep 10 '15 at 04:44