OK I have looked and looked and I think I am missing something about custom keyboards and inserting text into a UITextField
. Like many people I am creating a UIView
custom keyboard with my own set of key caps on custom UIButtons
. I then assign this view to the UITextField's inputView
and then my keyboard pops up when that text field becomes the first responder. Internal to the keyboard I capture button actions and translate them to what would be keystrokes.
But from here I am puzzled about how to best send my keystrokes to the text field. I have seen plenty of examples like iOS Custom Keyboard Updating Text Field In Parent View and Return Inputs To UITextfield With Custom Inputview and How To Push Key Value Into UITextView From Custom Keyboard In iOS where the solution seems to be to embed a pointer to the target UITextField
inside the custom view.
This does not seem right to me, as for multiple UITextFields
either I have to have to manually change references on a single custom keyboard whenever a new text field becomes the first responder, or I have to carry around a matching custom keyboard for each text field I want to support.
It seems to me that a keyboard should just translate touches into tokens and send those tokens up the line to be interpreted by whoever receives them (first responder?). iPad For iPhone Developers 101 Custom Input View Tutorial seems to do this, but creates a custom protocol to do so. I imagine (actually a SWAG - Scientific Wild Arsed Guess) that the standard iOS keyboards operate like that, but use a specific protocol to pass along the keystrokes. So what I want to be able to do is to manually generate the same chain of events/messages that the standard keyboard does.
So how can I do this? Or can I even do this with public APIs? Or is the operation of the standard keyboards bound too tightly to the UITextField?
This is a very open ended question. Really, it's up to you to pick what you want. Apple's documentation states this:
The subviews of an input view and input accessory view can be anything you want. If they are buttons or other controls, you need to specify targets and actions for each control and implement the associated action methods to perform data input or manipulation.
You could also make a UITextField subclass and explicitly implement the getter for inputView
to return a pointer to your custom keyboard view class. In this getter you can set up a UIControl-like target/action system as you suggest earlier. Doing it this way removes the need to set the custom keyboard from the outside--it's all handled internally. It also puts that code into only one place.
Once the custom text field resigns first responder, you can strip the text field's pointer out of the keyboard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With