【问题标题】:Resign First Responder on ScrollView Touch辞去 ScrollView Touch 上的第一响应者
【发布时间】:2011-04-07 16:16:28
【问题描述】:

如何在 ScrollView 触摸事件中隐藏键盘...

场景是这样的……

->视图->滚动视图->文本域

我想在触摸滚动视图时隐藏键盘。我试图覆盖滚动视图的类,但我仍然做不到。

【问题讨论】:

  • 我认为您需要使用委托。
  • 还有谁能帮我解决这个问题???

标签: iphone objective-c iphone-sdk-3.0 ios-simulator


【解决方案1】:

这样做会有所帮助:

@interface MyClass <UIScrollViewDelegate> {
}

@implementation

- (void)viewDidLoad {
  yourScrollView.delegate = self;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  [myTextField resignFirstResponder];
}

如果你真的想处理触摸事件,那么你需要继承 UIScrollView 并重写该方法:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {

}

更多关于UIScrollView touch

【讨论】:

  • 我需要让键盘在触摸时隐藏,而不是在拖动 ScrollView 时...所以有谁能帮我解决这个问题!!!
  • ...willBeginDragging 事件仅在手指拖动时触发,因此不能用于响应点击。
【解决方案2】:

这对我有用

在你的 viewController 的 viewDidLoad 方法中

    UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped)];
    tapScroll.cancelsTouchesInView = NO;
    [viewScroller addGestureRecognizer:tapScroll];

其中 viewScroller 是您的滚动条。在窃听方法中,我们有

    - (void) tapped {
        [self.view endEditing:YES];
    }

不知道为什么,但上面的方法对我不起作用......即使它应该

【讨论】:

  • 是的,在 iOSSDK 中使用手势之后,这是一个很好的解决方案,正如 @Dax 所回答的那样。但是这个问题很早就被问到了,那里没有手势。但是我这边 +1。
【解决方案3】:

试试这个:

为滚动视图添加手势识别器,

UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
    tapScroll.cancelsTouchesInView = NO;
    [yourScrollview addGestureRecognizer:tapScroll];
    [tapScroll release];

以(轻敲:)方法退出您的键盘。

【讨论】:

    【解决方案4】:

    请看一下这个答案,这是我找到的最简单的答案。

    UitextField resignFirstResponder does not work in scroll view

    【讨论】:

      【解决方案5】:
      - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
      {
          [self.view endEditing:YES];
           return YES;
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-17
        • 2017-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多