I wanted to put an input in an accessory view so that it was attached to the keyboard. Here's how I did it:
- Hidden input field, used only to launch the keyboard
- Button for "add item" makes the hidden input the first responder. This opens the keyboard with the configured accessory view
- The configured accessory view is loaded from a xib. it contains a label, background, and a UITextField with some configured options, like don't autocorrect and have a clear button.
- I tell the UITextField inside the accessory view to be the first responder, because I want the focus there when the user types.
- When the user is done, I resignFirstResponder on both the visible text field in the accessory view and the hidden one that launched the keyboard in the first place. I tried doing it to only one or the other--didn't dismiss the keyboard.
This all works--I can read the text out of the field, change my button between add
and cancel
, and validate input the user provides.
Here's what that looks like:
The only thing that doesn't work is the cancel button. It appears when it should, but it never detects taps. Is there something known and broken about clear buttons in accessory input fields? I can't find anything.
I tried using the textFieldShouldClear
delegate method, but it never fires. Other delegate methods (textFieldShouldReturn
, shouldChangeCharactersInRange
) work fine.