ios UITextView 数据变化迅速
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25064465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITextView data change swift
提问by user3896519
How can you detect a change of data in a UITextView
with Swift
? The following code does not do any detection.
如何检测UITextView
with 中的数据变化Swift
?以下代码不做任何检测。
I am declaring the UITextView
:
我声明UITextView
:
@IBOutlet weak var bodyText: UITextView!
optional func textViewDidChange(_ textView: UITextView!) {
println(bodyText.text)
}
Thanks Scott
谢谢斯科特
回答by Sergey Pekar
You need to set UITextView delegateand 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.
您需要设置 UITextView委托并在其中实现textViewDidChange:方法。不幸的是,我不知道 swift 文档是否在线可用。所有链接都转到objective-c 文档。
The code will look like this: (updated for SWIFT 4.2)
代码将如下所示:(针对 SWIFT 4.2 更新)
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
}
}
回答by codester
Set delegate
of UITextView
.Refer UITextViewDelegate
设置delegate
的UITextView
.Refer UITextViewDelegate
Write this in viewDidLoad
把这个写在 viewDidLoad
bodyText!.delegate = self
回答by Lucas Chwe
For my case, I wanted the implementation to be independent from a UIViewController, so i dont need to assign a delegate just for text changes. Or even maybe there is some kind of validation on the UITextView, and you wanna contain it per field instead of a delegate managing a lot of complicated logic.
就我而言,我希望实现独立于 UIViewController,因此我不需要仅为文本更改分配委托。或者甚至可能对 UITextView 进行某种验证,并且您希望每个字段都包含它,而不是管理大量复杂逻辑的委托。
It requires to subclass UITextView, but its very much worth it imo:
它需要继承 UITextView,但它非常值得 imo:
class TextView: UITextView {
convenience init() {
self.init(frame: CGRect.zero, textContainer: nil)
NotificationCenter.default.addObserver(self, selector: #selector(textDidChangeNotification), name: UITextView.textDidChangeNotification , object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc func textDidChangeNotification(_ notif: Notification) {
guard self == notif.object as? UITextView else {
return
}
textDidChange()
}
func textDidChange() {
// the text in the textview just changed, below goes the code for whatever you need to do given this event
// or you can just set the textDidChangeHandler closure to execute every time the text changes, useful if you want to keep logic out of the class
textDidChangeHandler?()
}
var textDidChangeHandler: (()->Void)?
}
回答by Andy
For Swift 4:
对于 Swift 4:
func textViewDidChange(_ textView: UITextView) {
// Your code here
}