【问题标题】:How to have UITextView becomeFirstResponder after touching its UIView subview?触摸 UIView 子视图后如何让 UITextView 成为FirstResponder?
【发布时间】:2011-06-02 22:28:43
【问题描述】:

更新:我找到了simple solution。我知道有一个!

我有一个名为tipBalloonUIView 子类,我已将其添加为UITextView 的子视图,但是当我触摸tipBalloon 时,即使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;
}

【问题讨论】:

    标签: iphone cocoa-touch uiview uitextview becomefirstresponder


    【解决方案1】:

    更新:更简单的解决方案是设置tipBalloon.userInteractionEnabled = NO。这会导致tipBalloon 忽略任何触摸,然后将其传递给UITextView。我在观看WWDC 2011 video 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];
    

    【讨论】:

    • 不错的 +1,但我的解决方案的目的是可重复使用。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      在你的tipBalloonviewDidAppear: 里面你可以放:

      [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 设置为第一响应者。

      【讨论】:

      • 谢谢,但不应该将tipBalloon 的触摸自动转发到UITextView,因为tipBalloonUITextView 的子视图?
      • 你设置tipBalloon.exclusiveTouch = NO了吗?但是,这应该是默认值,正如您所说,人们应该期望“汇总”到UITextView。老实说,我不确定为什么现在不这样做。
      • 不,我没有设置tipBalloon.exclusiveTouch = NO。并且,[tipBalloon nextResponder] 返回UITextView。也许这是一个 UIKit 错误。无论如何,您的解决方案奏效了!谢谢!我tidied it up a bit.
      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 2012-01-06
      • 2012-01-31
      • 2014-06-21
      • 2010-11-19
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多