0
votes

I wanted to put an input in an accessory view so that it was attached to the keyboard. Here's how I did it:

  1. Hidden input field, used only to launch the keyboard
  2. Button for "add item" makes the hidden input the first responder. This opens the keyboard with the configured accessory view
  3. 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.
  4. I tell the UITextField inside the accessory view to be the first responder, because I want the focus there when the user types.
  5. 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:

accessory input view

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.

1

1 Answers

0
votes

Owch. This was all because way up at the top-level view in the XIB I set "User Interaction Enabled" to false. It never occurred to me that this cascades to child views, including native controls, but of course this is the case.

I had originally disabled user interaction so that I didn't have to manually size the accessory view--it could be full screen and taps would just go through it to the UI behind. Now, I enable user interaction, and size the view to a small height after pulling it out of the xib and everything works great.