自定义UITextField委托方法shouldChangeCharactersIn没有被调用

时间:2018-07-11 11:37:09

标签: ios swift uitextfield uitextfielddelegate

在我的自定义UITextField中,textFieldDidBeginEditing()textFieldDidEndEditing()委托方法被调用,但是 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool

@IBDesignable class CustomUITextField: UITextField, UITextFieldDelegate{

   //getting Called
   @objc func textFieldDidBeginEditing() {

    }

   //getting Called
    @objc func textFieldDidEndEditing(){

    }

    //Not getting Called
    @objc func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    }

}

我不知道为什么没有接到电话。为此需要做任何额外的工作吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

您需要设置代表

@IBDesignable class CustomUITextField: UITextField, UITextFieldDelegate{

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.delegate = self
    }

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

          self.delegate = self
    }

    //getting Called
    @objc func textFieldDidBeginEditing() {

        print("textFieldDidBeginEditing")
    }

    //getting Called
    @objc func textFieldDidEndEditing(){

        print("textFieldDidEndEditing")
    }

    //Not getting Called
    @objc func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        print("shouldChangeCharactersIn")

        return true

    }

}