如何让 UITextView 在触摸其 UIView 子视图后成为FirstResponder?
更新:我发现了简单的解决方案。我就知道有一个!
我有一个名为 tipBalloon
的 UIView
子类,我已将其添加为 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新:更简单的解决方案是设置
tipBalloon.userInteractionEnabled = NO
。这会导致tipBalloon
忽略任何触摸,然后将其传递给UITextView
。我在观看 @elizablock 学到了这项技术/iqpttM" rel="nofollow">WWDC 2011 视频高级滚动视图技术。这是一个基于@dredful 的解决方案(只是稍微整理了一下)。在
[tipBalloon release];
之前添加以下代码块:UPDATE: A simpler solutions is just to set
tipBalloon.userInteractionEnabled = NO
. This causes thetipBalloon
to ignore any touches, which then get passed to theUITextView
. 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];
:如果
UIView
是 textView 的子视图,您应该能够调用[tipBalloon.superview comeFirstResponder]
If the
UIView
is a subview of your textView, you should be able to call[tipBalloon.superview becomeFirstResponder]
如果我直接理解你的问题,你需要注册与
UIView
的交互。对于UIView
,最好将UITapGestureRecognizer
添加到tipBalloon
的tipBalloon
的内部>viewDidAppear:
你可以把:在你的
tipBalloon
中有这些方法,这将向你的
tipBalloon
UIView 添加一个点击。当点击触发时,它可以将 UITextView 设置为第一响应者。If I understand your question directly, you need to register the interaction with the
UIView
. For aUIView
it is probably best to add aUITapGestureRecognizer
to yourtipBalloon
Inside of your
tipBalloon
'sviewDidAppear:
you can put:The have these methods in your
tipBalloon
This will add a tap to your
tipBalloon
UIView. When the tap fires it can then set theUITextView
as the first responder.