运行另一个textViewDidChange

问题描述 投票:0回答:1

如果我在UITextView的扩展中运行textViewDidChange函数,我不能在其他类中使用textViewDidChange

请告诉我解决方案。

代码如下。 (它已被简化。)

extension UITextView: UITextViewDelegate {
    initValue(){
        //Start this method at first.
        self.delegate = self
    }

    //1. If already set this method in this extension
    public func textViewDidChange(_ textView: UITextView) {
        print("Do something!!")
    }
}


class A: UITextViewDelegate{
    @IBOutlet weak var textView: UITextView!

    func initValue(){
        textView.delegate = self
    }

    //2. This method don't move
    func textViewDidChange(_ textView: UITextView) {
        print("Nein!! wan can't do anything!!")
    }
}
ios swift uitextview
1个回答
0
投票

使用如下的闭包定义方法:

func textViewDidChange(_ textView: UITextView, _  completion: (() -> Swift.Void)? = nil){
    completion?()
    return
}

订阅viewDidLoad()中的通知,例如:

textField.addTarget(self, action: #selector(doChanges(_:)), for: .editingChanged)

并将该函数添加到该viewController,它允许您在完成时执行操作

@objc func doChanges() {
    textFieldDidChange(self.textField) { 
        //doCustomWorkHere
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.