代码之家  ›  专栏  ›  技术社区  ›  user3896519

UITextView数据快速更改

  •  45
  • user3896519  · 技术社区  · 11 年前

    如何检测 UITextView 具有 Swift ? 以下代码不进行任何检测。

    我宣布 UI文本视图 :

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

    谢谢 斯科特

    6 回复  |  直到 11 年前
        1
  •  112
  •   zeeshan    6 年前

    您需要设置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
  •  6
  •   Lucas Chwe    6 年前

    就我的情况而言,我希望实现独立于UIViewController,因此我不需要仅为文本更改分配代理。甚至可能UITextView上有某种验证,您希望每个字段都包含它,而不是委托管理大量复杂的逻辑。

    它需要子类UITextView,但它非常值得:

    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
  •  5
  •   codester    11 年前

    设置 delegate 属于 UITextView 参考 UITextViewDelegate

    将此写入 viewDidLoad

    bodyText!.delegate = self
    
        4
  •  4
  •   Andy    7 年前

    对于Swift 4:

    func textViewDidChange(_ textView: UITextView) {
      // Your code here
    }
        5
  •  2
  •   Fattie    3 年前

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

    对于Swift5,“name”值的格式为 UITextView.textDidChangeNotification .

        6
  •  0
  •   Fattie    3 年前

    子类化UITextView时:

    import UIKit
    class SelfAwareTextView: UITextView {
        
        func setup() {
            NotificationCenter.default.addObserver(self,
             selector: #selector(change),
             name: UITextView.textDidChangeNotification,
             object: self)
        }
        
        @objc func change(_ notif: Notification) {
            print("the user edited the text. It's now: " + text)
        }
    }
    

    在添加观察者时 object: 是发送对象。

    有点困惑,你把它设置为 self 因为,事实上,我们只是想要自己的通知;我们不关心其他文本视图。

    关于 setup ,请记住(通常)在对UITextView进行子类化时 awakeFromNib 而不是init等,因为在其他问题中,您可以捕获任何IBInspectable值。