运行另一个textViewDidChange

时间:2018-03-03 02:45:27

标签: ios swift uitextview

如果我在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!!")
    }
}

1 个答案:

答案 0 :(得分: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
    }
}