UITextView数据变化迅速 [英] UITextView data change swift
本文介绍了UITextView数据变化迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 Swift
检测 UITextView
中的数据更改?以下代码不会进行任何检测。
How can you detect a change of data in a UITextView
with Swift
? The following code does not do any detection.
我声明 UITextView
:
@IBOutlet weak var bodyText: UITextView!
optional func textViewDidChange(_ textView: UITextView!) {
println(bodyText.text)
}
谢谢
Scott
Thanks Scott
推荐答案
你需要设置UITextView < a href =https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate>委托并实施 textViewDidChange:方法。不幸的是,我不知道是否可以在线获得快速文档。所有链接都转到objective-c文档。
You need to set UITextView delegate and implement textViewDidChange: method in it. Unfortunately, I do not know if swift documentation is available online. All the links go to the objective-c documentation.
代码看起来像这样
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
}
}
这篇关于UITextView数据变化迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文