Подтвердить что ты не робот

Быстрое изменение данных UITextView

Как вы можете обнаружить изменение данных в UITextView с помощью Swift? Следующий код не обнаруживает.

Я объявляю UITextView:

@IBOutlet weak var bodyText: UITextView!

optional func textViewDidChange(_ textView: UITextView!) {
    println(bodyText.text)
}

Спасибо Скотт

4b9b3361

Ответ 1

Вам нужно установить UITextView delegate и реализовать в нем textViewDidChange:. К сожалению, я не знаю, доступна ли оперативная документация онлайн. Все ссылки переходят к документации objective-c.

Код будет выглядеть следующим образом:

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

Ответ 2

Установите delegate из UITextView.Refer UITextViewDelegate

Запишите это в viewDidLoad

bodyText!.delegate = self