2

UPDATE: I found a simple solution. I knew there was one!

I have a UIView subclass called tipBalloon that I've added as a subview of a UITextView, but when I touch tipBalloon, it doesn't focus the text view even though tipBalloon.userInteractionEnabled = YES (by default).

How do I make it so that when I touch tipBalloon, the touch is forwarded to the UITextView? Shouldn't this happen automatically?

Here is an example:

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UITextView *payTextView = [[UITextView alloc] initWithFrame:CGRectMake(5.0f, 30.0f, 180.0f, 100.0f)];
    [window addSubview:payTextView];

    UIView *tipBalloon = [[UIView alloc] initWithFrame:CGRectMake(6.0f, 10.0f, 100.0f, 30.0f)];
    tipBalloon.backgroundColor = [UIColor orangeColor];
    [payTextView addSubview:tipBalloon];
    [tipBalloon release];

    window.backgroundColor = [UIColor brownColor];
    [window makeKeyAndVisible];
    return YES;
}
Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651

3 Answers3

3

UPDATE: A simpler solutions is just to set tipBalloon.userInteractionEnabled = NO. This causes the tipBalloon to ignore any touches, which then get passed to the UITextView. I learned this technique from @elizablock while watching the WWDC 2011 video Advanced Scroll View Techniques.

Here's a solution based on @dredful's (just tidied up a bit). Add the following block of code just before [tipBalloon release];:

// Focus payTextView after touching payTextExample.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:
                                     payTextView action:@selector(becomeFirstResponder)];
[tipBalloon addGestureRecognizer:singleTap];
[singleTap release];
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
1

If the UIView is a subview of your textView, you should be able to call [tipBalloon.superview becomeFirstResponder]

justin
  • 5,811
  • 3
  • 29
  • 32
1

If I understand your question directly, you need to register the interaction with the UIView. For a UIView it is probably best to add a UITapGestureRecognizer to your tipBalloon

Inside of your tipBalloon's viewDidAppear: you can put:

[self addGestureRecognizerToUIView:self.view];

The have these methods in your tipBalloon

- (void)addGestureRecognizerToUIView:(id)thisUIView
{
    // Single Tap
    UITapGestureRecognizer *thisTap = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                               action:@selector(handleTap:)];
    thisTap.numberOfTapsRequired = 1;
    [thisUIView addGestureRecognizer:thisTap];
    [thisTap release];

}

- (void)handleTap:(UITapGestureRecognizer *)gesture {
    [yourUITextView becomeFirstResponder];
}

This will add a tap to your tipBalloon UIView. When the tap fires it can then set the UITextView as the first responder.

dredful
  • 4,378
  • 2
  • 35
  • 54
  • Thank you, but shouldn't the touch on `tipBalloon` be forwarded to the `UITextView` automatically since `tipBalloon` is a subview of the `UITextView`? – ma11hew28 Jun 03 '11 at 00:33
  • Have u set `tipBalloon.exclusiveTouch = NO`? However, that should be the default which one should expect would "roll up" to the `UITextView` as you say. Honestly I am not sure why it is not doing so now. – dredful Jun 03 '11 at 00:40
  • No, I didn't set `tipBalloon.exclusiveTouch = NO`. And, `[tipBalloon nextResponder]` returns the `UITextView`. Maybe this is a UIKit bug. Anyway, your solution worked! Thanks! I [tidied it up a bit](http://stackoverflow.com/questions/6221109/how-to-have-uitextview-becomefirstresponder-after-touching-its-uiview-subview/6234110#6234110). – ma11hew28 Jun 04 '11 at 01:49