我对UITextView进行了子类化,并在像textViewDidChangeSelection这样的子类中实现了一些委托方法,但我还需要在视图控制器中为UITextView委托获取通知。因此,如果我创建了子类的对象并在视图控制器中设置了textview委托,那么委托方法只会在视图控制器中被通知,而不会在子类中被通知。我需要通知两个班级。我使用的语言是swift 2
我尝试在子类委托中继承UITextViewDelegate:
@objc protocol CustomTextViewDelegate:UITextViewDelegate {
func customTextViewDidChangeSize(chatTextView: CustomTextView)
}然后在VC中:
let customTV = CustomTextView()
customTV.customTextViewDelegate = self但是任何文本视图委托方法都不会被调用。
发布于 2020-09-29 20:28:26
问得好。这是一个不太好的答案,因为它需要你重写所有的委托方法,因此在iOS版本中不稳定,以防委托方法随着时间的推移而改变。
在这种方法中,ViewController和CustomTextField都可以访问委托事件。
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
}现在,ViewController和CustomTextField都可以访问UITextFieldDelegate。
发布于 2016-03-27 05:27:04
不能同时将两个对象委托给UITextView对象。因此,您应该为CustomTextView创建新的协议(CustomTextViewDelegate),并在其中创建委托属性。让你的ViewController确认这个CustomTextViewDelegate并实现它的方法。在CustomTextView的UITextViewDelegate方法实现中,可以调用适当的CustomTextViewDelegate方法。
发布于 2021-05-02 03:11:07
@nayooti的方法很好,但是我必须做一些修复才能正常工作,主要的一个是一些委托方法不会被调用(例如shouldChangeTextIn range和textViewDidChange )。
我发现可以通过在被覆盖的delegate变量的get访问器上返回super.delegate而不是superDelegate来修复它。
class CustomTextView: UITextView {
override var delegate: UITextViewDelegate? {
set {
superDelegate = newValue
} get {
return super.delegate
}
}
private weak var superDelegate: UITextViewDelegate?
init() {
super.init(frame: .zero, textContainer: nil)
super.delegate = self
}
override public init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
super.delegate = self
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
super.delegate = self
}
func textDidChange(text: String?) {
// Do something
}
}
extension CustomTextView: UITextViewDelegate {
public func textViewDidChange(_ textView: UITextView) {
// Catch text-change events here
textDidChange(text: textView.text)
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) ?? true
}
public func textViewDidBeginEditing(_ textView: UITextView) {
superDelegate?.textViewDidBeginEditing?(textView)
}
public func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
return superDelegate?.textViewShouldEndEditing?(textView) ?? true
}
public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return superDelegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) ?? true
}
public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return superDelegate?.textView?(textView, shouldInteractWith: URL, in: characterRange, interaction: interaction) ?? true
}
public func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return superDelegate?.textView?(textView, shouldInteractWith: textAttachment, in: characterRange, interaction: interaction) ?? true
}
}https://stackoverflow.com/questions/36240452
复制相似问题