views:

347

answers:

1

I'm currently working on an app with this structure:

I have a view controller which we will call MainViewController, that handles all touch events for the application. MainViewController has as an instance variable, a view called canvasView. canvasView has as a UITextView called hiddenTextView.

Theres other invisible views and such so UITextView is not the first to register touches.

How do I get the UITextView to register touches in this configuration? Specifically to make selections from the autoCorection box (Actually its for Japanese Kanji selection, but I assume its a similar mechanism).

I've tried:

[canvasView.hiddenTextView touchesBegan:touches withEvent:event]

from the MainViewController's touchesbegan method. I've done this for touchesBegan, moved, and Ended.

I think it actually worked for one out of the 50 or so runs I've tried, but I don't know what changed.

Any ideas on how I can get this to work?

A: 
[canvasView.hiddenTextView becomeFirstResponder];

will probably do what you want.

The responder chain on the iPhone is a slightly tricky thing. I find it's usually easier to avoid overlapping controls, so that at any given instance it's completely unambiguous as to which control should receive input from which touches.

If you're overlapping a lot of controls, and often switching between which ones are hidden or visible, that's a good sign that you might want to just implement more subclasses of UIViewController, and switch between those instead of swapping in out and the controls. Another option is to make custom subclasses of UIView and swap those in and out instead -- by which I mean, actually remove the old one from the subview (not hiding it), and adding the new one.

Of course, there are some times when hiding/showing is just a lot easier than subclassing.

I hope that helps. Reference: Apple's UIResponder docs. (Note that any UIViewController or UIView is a subclass of UIResponder.)

Tyler
The UITextView is already the first responder at that point though, the keyboard is up and everything.Tried it anyways, but still nothing :(.I've tried using [[self nextResponder] touchesBegan.....] too, which has worked for me in other cases of passing touches down to other layers but not this time.
kiyoshi
Another idea: double-check the frame of your UITextView and its superviews. I had a case where my control was clearly visible, but not getting any touches. The problem turned out to be that its frame had size (0,0). It looks like responders don't get touches unless the touches are in their frame.
Tyler
Hey Tyler, thanks for ur responses.I ended up just moving all the hiddenTextView stuff to the top view controller, and making sure the hiddenTextView was the frontmost subview. Then the UIResponder stuff handled itself, and after making it the first responder, no further touch forwarding was needed. I wish the UIKit API was a little more transparent though :(
kiyoshi
Glad you got it! Sorry if I couldn't provide a quicker fix.
Tyler