如何让 UITextView 在触摸其 UIView 子视图后成为FirstResponder?

发布于 2024-11-13 06:17:50 字数 1197 浏览 0 评论 0 原文

更新:我发现了简单的解决方案。我就知道有一个!

我有一个名为 tipBalloonUIView 子类,我已将其添加为 UITextView 的子视图,但是当我触摸 tipBalloon 时code>,即使 tipBalloon.userInteractionEnabled = YES(默认情况下),它也不会聚焦文本视图。

如何才能在触摸 tipBalloon 时将触摸转发到 UITextView?这不应该自动发生吗?

这是一个例子:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病毒体 2024-11-20 06:17:50

更新:更简单的解决方案是设置tipBalloon.userInteractionEnabled = NO。这会导致 tipBalloon 忽略任何触摸,然后将其传递给 UITextView。我在观看 @elizablock 学到了这项技术/iqpttM" rel="nofollow">WWDC 2011 视频高级滚动视图技术。

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

// Focus payTextView after touching payTextExample.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:
                                     payTextView action:@selector(becomeFirstResponder)];
[tipBalloon addGestureRecognizer:singleTap];
[singleTap release];

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];
梦一生花开无言 2024-11-20 06:17:50

如果 UIView 是 textView 的子视图,您应该能够调用 [tipBalloon.superview comeFirstResponder]

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

朱染 2024-11-20 06:17:50

如果我直接理解你的问题,你需要注册与UIView的交互。对于 UIView ,最好将 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];
}

,这将向你的 tipBalloon UIView 添加一个点击。当点击触发时,它可以将 UITextView 设置为第一响应者。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文