在子类和 UIViewController 中使用 UITextViewDelegate

2024-01-24

我正在子类化 UITextView 并在子类中实现一些委托方法,例如textViewDidChangeSelection但我还需要在视图控制器中获取 UITextView 委托的通知。 因此,如果我创建子类的对象并在视图控制器中设置 textview 委托,则仅在视图控制器中而不是在子类内通知委托方法。我需要通知两个班级。我使用的语言是 swift 2

我尝试在子类委托中继承 UITextViewDelegate :

@objc protocol CustomTextViewDelegate:UITextViewDelegate {

    func customTextViewDidChangeSize(chatTextView: CustomTextView)

}

然后在VC中:

let customTV = CustomTextView()
customTV.customTextViewDelegate = self

但任何 textview 委托方法都不会被调用。


好问题。这是一个不太好的答案,因为它需要您重写所有委托方法,因此在 iOS 版本之间不稳定,以防委托方法随着时间的推移而变化。

在这种方法中ViewControllerCustomTextField两者都可以访问委托事件。

class CustomTextView: UITextView {
   override var delegate: UITextViewDelegate? {
      set {
         superDelegate = newValue
      } get {
         return superDelegate
      }
    }

    private weak var superDelegate: UITextViewDelegate?

    init() {
       super.init(frame: .zero, textContainer: nil)
       super.delegate = self
    }

    func textDidChange(text: String?) {
        // do something
    }

}

extension BoundTextView: UITextViewDelegate {
    public func textViewDidChange(_ textView: UITextView) {
        // catch text-change events here
        textDidChange(text: String?) 
        superDelegate?.textViewDidChange?(textView)
    }

    public func textViewDidEndEditing(_ textView: UITextView) {
        superDelegate?.textViewDidEndEditing?(textView)
    }

    public func textViewDidChangeSelection(_ textView: UITextView) {
        superDelegate?.textViewDidChange?(textView)
    }

    public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
       return superDelegate?.textViewShouldBeginEditing?(textView) ?? false
    }

    public func textViewDidBeginEditing(_ textView: UITextView) {
        superDelegate?.textViewDidBeginEditing?(textView)
    }

    public func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
       return superDelegate?.textViewShouldEndEditing?(textView) ?? false
    }

    public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        return superDelegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) ?? false
    }

    public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        return superDelegate?.textView?(textView, shouldInteractWith: URL, in: characterRange, interaction: interaction) ?? false
    }

    public func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        return superDelegate?.textView?(textView, shouldInteractWith: textAttachment, in: characterRange, interaction: interaction) ?? false
    }
}

我们重写delegate并将对其的引用存储在一个单独的变量中(称为superDelegate)。 CustomTextField 将自身分配给super.delegate并实施UITextView-delegate。我们必须确保每个委托事件都会触发相应的事件superDelegate的活动。

我们的“ViewController”现在可以将自己指定为CustomTextView的代表:

class ViewController: UIViewController {

   ...
   lazy var textField: CustomTextView {
      let textView = CustomTextField()
      textView.delegate = self 
      return textField
   }()
   ...


}

extension ViewController: UITextViewDelegate {

   // implement only the delegate-methods you need

}

现在两者,ViewControllerCustomTextField,都可以访问UITextFieldDelegate.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在子类和 UIViewController 中使用 UITextViewDelegate 的相关文章

随机推荐