Reputation: 6998
I've built a custom on-screen keyboard where each key tap returns a String?
. In my delegate method, I want to forward that text onto the UITextFieldDelegate
method: func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
func valueWasSent(string: String?) {
// activeTextField is a UITextField?
guard let textField = activeTextField else { return }
let range = NSMakeRange(0, string!.count - 1)
textField(activeTextField!, shouldChangeCharactersIn: range, replacementString: string!)
}
The last line here isn't working, and throwing the error:
Cannot call value of non-function type 'UITextField'
Any idea how I can accomplish this? Thanks
Upvotes: 0
Views: 948
Reputation: 318774
Most likely you should not be calling that delegate method yourself. The shouldChangeCharactersIn
is simply a check if the edit would be valid. Normally, UITextField
calls it to ensure the proposed change will be valid or not. If not, the change is ignored. If so, then the text field's text is updated.
If you really do want to call it, then you need to call it on the delegate, not self
. You need to check the return value. If it returns true
then you should update the text field's text accordingly.
if textField.delegate?.textField(textField, shouldChangeCharactersIn: range, replacementString: string!) {
// update the text field's text
}
Also note that the range you pass to it should reflect the current selection in the text field. You are setting it to match the length of string
. That's bad. If you want string
to fully replace the current value, at least pass in a range that matches the current value of the text field, not of string
.
Upvotes: 1