0

我在顶部单视图上有一个滚动视图。我有一些文本字段和UIPickers它。现在我知道如何在按下返回时让键盘关闭。但是,当点击或选择背景时,我试图从文本字段中关闭键盘UIpicker。我试过这样做...

界面 :

  - (IBAction)textFieldReturn:(id)sender;
  - (IBAction)backgroundTouched:(id)sender;

执行 :

-(IBAction)textFieldReturn:(id)sender
{
        [sender resignFirstResponder];
}

-(IBAction)backgroundTouched:(id)sender
{
        [textField resignFirstResponder];
}

但问题是我不能将滚动视图作为控件类型来使其工作..

4

1 回答 1

1

试试这样可能对你有帮助但不确定,

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
       if (![[touch view] isKindOfClass:[UITextField class]]) {
           [yourtextfield resignFirstResponder];
       }
}

为了在滚动视图上获得触摸事件,您必须进行手势识别,

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    [scroll addGestureRecognizer:singleTap];

 -(void)singleTap{
        [text resignFirstResponder];
        //write whatever you want it.
    }
于 2013-03-11T04:00:46.980 回答