Textview没有回应它的各自功能

时间:2015-09-10 03:26:27

标签: ios swift textview

我在看Text View Placeholder Swift。在我看来,我做了这篇文章告诉我要做的所有事情。但是,它不起作用。更具体地说,textViewDidBeginEditing()textViewDidEndEditing()函数没有执行。我之所以认为这是因为我写了println("hello")println("YO"),但它没有在控制台上打印任何内容。有人有办法解决这个问题吗?

这是我的代码:

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.
    }

}

1 个答案:

答案 0 :(得分:1)

您确定UITextView的委托是否已正确分配?例如viewDidLoad中的writePrayerRequestTextView.delegate = self