UITextView 数据变化迅速
Posted
技术标签:
【中文标题】UITextView 数据变化迅速【英文标题】:UITextView data change swift 【发布时间】:2014-09-23 17:26:19 【问题描述】:如何使用Swift
检测UITextView
中的数据变化?以下代码不做任何检测。
我声明UITextView
:
@IBOutlet weak var bodyText: UITextView!
optional func textViewDidChange(_ textView: UITextView!)
println(bodyText.text)
谢谢 斯科特
【问题讨论】:
此 UITextView 占位符解决方案提供了一个工作示例:***.com/questions/27652227/… 【参考方案1】:您需要设置 UITextView delegate 并在其中实现 textViewDidChange: 方法。不幸的是,我不知道 swift 文档是否可以在线获得。所有链接都指向 Objective-c 文档。
代码将如下所示:(已针对 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
【讨论】:
此方法是否仅在文本值更改时调用,还是也响应属性文本属性(粗体/斜体/等)的更改? 没有测试。但似乎该方法会响应文本中的任何更改【参考方案2】:就我而言,我希望实现独立于 UIViewController,因此我不需要为文本更改分配委托。或者甚至可能对 UITextView 进行某种验证,并且您希望每个字段都包含它,而不是委托管理许多复杂的逻辑。
它需要继承 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)?
【讨论】:
【参考方案3】:设置delegate
的UITextView
。参考UITextViewDelegate
写在viewDidLoad
bodyText!.delegate = self
【讨论】:
【参考方案4】:对于 Swift 4:
func textViewDidChange(_ textView: UITextView)
// Your code here
【讨论】:
【参考方案5】:对于 Swift 4.2,您可以像这样将 UITextViewTextDidChange
、UITextViewTextDidBeginEditing
和 UITextViewTextDidEndEditing
通知观察者添加到 UITextView:
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(textViewDidChange), name: NSNotification.Name.UITextViewTextDidChange , object: nil)
nc.addObserver(self, selector: #selector(textViewDidBeginEditing), name: NSNotification.Name.UITextViewTextDidBeginEditing , object: nil)
nc.addObserver(self, selector: #selector(textViewDidEndEditing), name: NSNotification.Name.UITextViewTextDidEndEditing , object: nil)
观察者看起来像这样:
@objc func textViewDidChange(_ notif: Notification)
guard notif.object is UITextView else return
// Do something
@objc func textViewDidBeginEditing(_ notif: Notification)
guard notif.object is UITextView else return
// Do something
@objc func textViewDidEndEditing(_ notif: Notification)
guard notif.object is UITextView else return
// Do something
【讨论】:
以上是关于UITextView 数据变化迅速的主要内容,如果未能解决你的问题,请参考以下文章
UILabel UITextField UITextView