Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to send key to UITextField?

I have implemented a custom keyboard view in an iOS application. I have several UITextFields that use this keyboard. Some of these UITextFields have delegates that override shouldChangeCharactersInRange. However if my keyboard just sets the text value in the text field, the shouldChangeCharactersInRange message is not sent. What I think I need is to actually do something like SendKey and send the key code to the UITextField.

Any suggestions?

like image 705
dakamojo Avatar asked May 04 '11 15:05

dakamojo


People also ask

What is Uitextfield in Swift?

An object that displays an editable text area in your interface.

How do you dismiss a keyboard in Swift?

Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing .


1 Answers

As I note in my answer here, the UITextField conforms to the UITextInput protocol. So you call its methods:

[textField replaceRange:textField.selectedTextRange withText:text];

Of interest: "" will perform a backspace and, of course, " " will do a space.

This should call the shouldChangeCharactersInRange method automatically. If not, you'd have to do that yourself.

You definitely need to do this if you are going to create a custom keyboard on iOS.

like image 192
Dan Rosenstark Avatar answered Nov 15 '22 06:11

Dan Rosenstark