在ScrollView Touch上重新启动First Responder

时间:2010-09-03 04:32:28

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

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

情景就是这样......

  

- >查看 - > ScrollView - > Textfield

我想在触摸scrollView时隐藏键盘。我试图覆盖scrollview的类,但我仍然无法做到。

5 个答案:

答案 0 :(得分:8)

这样做会有所帮助:

@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

的更多信息

答案 1 :(得分:6)

这对我有用

在viewController的viewDidLoad-method

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

其中viewScroller是您的滚动条。在我们的tapped方法中

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

不知道为什么,但上述内容对我不起作用......即使它应该

答案 2 :(得分:4)

试试这个:

为scrollview添加手势识别器,

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

将键盘重新设置为(tapped :)方法。

答案 3 :(得分:0)

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

UitextField resignFirstResponder does not work in scroll view

答案 4 :(得分:0)

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    [self.view endEditing:YES];
     return YES;
 }