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;
}
4

3 回答 3

3

更新:一个更简单的解决方案就是设置tipBalloon.userInteractionEnabled = NO。这会导致tipBalloon忽略任何触摸,然后将其传递给UITextView. 我在观看WWDC 2011 视频 Advanced Scroll View Techniques时从@elizablock学到了这项技术。

这是一个基于@dredful 的解决方案(只是整理了一下)。在之前添加以下代码块[tipBalloon release];

// Focus payTextView after touching payTextExample.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:
                                     payTextView action:@selector(becomeFirstResponder)];
[tipBalloon addGestureRecognizer:singleTap];
[singleTap release];
于 2011-06-04T01:33:42.720 回答
1

如果我直接理解您的问题,您需要注册与UIView. 对于 a UIView,最好将 a 添加UITapGestureRecognizer到您的tipBalloon

在你tipBalloon的里面viewDidAppear:你可以放:

[self addGestureRecognizerToUIView:self.view];

有这些方法在你的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];
}

这将为您的tipBalloonUIView 添加一个水龙头。当水龙头触发时,它可以将 设置UITextView为第一响应者。

于 2011-06-03T00:30:14.297 回答
1

如果这UIView是您的 textView 的子视图,您应该可以调用[tipBalloon.superview becomeFirstResponder]

于 2011-06-02T23:42:25.917 回答