views:

30

answers:

2

I have a somewhat complex iOS view hierarchy. One piece of text is an editable UITextField. When the user touches it, it becomes first responder, and is editable.

Here's the rub, though: Best practice should be that a touch anywhere outside the edit control causes it to resign first responder and end editing. What's the best way of accomplishing this?

Techniques I've tried:

  1. Use the exclusiveTouch property, which stops the user from interacting with other controls, but doesn't cause editing to end. Also disallows user from interacting with my toolbar "Done" button.
  2. Put a see-through UIView under the text field control and on top of everything else (except the toolbar), and use touches there to end editing. This works, but I end up reparenting the text field onto this other random view which sits above my whole hierarchy, which means I have to take care of the text field's layout in multiple places, since it no longer lives in the place where it lived originally, and I have to delegate all its behavior back and forth from its "shield" view to its native home container, which has all the related logic.

Is there an elegant solution to this problem that I'm missing? I figure it must be a common design issue.

Thanks.

A: 

Tile 4 "see-thru" views around the textview to capture/ignore touches. Doesn't require modifying or "lifting" the textview, and can be added to the parent view in a fairly modular way.

You can't mask a region without knowing what that mask will cover and what the mask will not cover. So any solution will require enough reach to gather both of those bounds. Either pass the text rect up, or the view rect/region to be disabled down, or both to something in-between. The controller for the stuff to be covered seems as good a place as any to consolidate both rects or regions, if not the controller for the text view.

hotpaw2
Thanks. Ah, but the textview is buried in my object hierarchy and doesn't know the bounds of the overall screen-- finding that out would just be reaching around the views again. More problematically, the text view might not be the "top" subview in the whole screen area-- if it's not, then adding peer views around it doesn't guarantee anything, right?
quixoto
You can't mask a region without knowing what that mask will cover and what the mask will not cover. So any solution will require enough reach to gather both of those bounds. Either pass the text rect up, or the view rect/region to be disabled down, or both to something in-between. The controller for the stuff to be covered seems as good a place as any to consolidate both rects or regions, if not the controller for the text view.
hotpaw2
Thanks for the discussion. I've added your insightful followup into your actual answer.
quixoto
A: 

If you change your base view to a UIControl you can add an IBAction to that layer that resigns your text field as first responder.

Also, if you have multiple touch events, make sure they each becomeFirstResponder when touched.

I'd love to have some more details to qualify my explanations xD

MishieMoo