我在顶部单一视图上有滚动视图。我有一些文本字段和UIPickers
。现在我知道如何在按下返回键时使键盘关闭。但是,当我点击背景或选择UIpicker
时,我试图从文本字段中取消键盘。我试过这样做......
界面:
- (IBAction)textFieldReturn:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
实施:
-(IBAction)textFieldReturn:(id)sender { [sender resignFirstResponder]; } -(IBAction)backgroundTouched:(id)sender { [textField resignFirstResponder]; }
但问题是我无法将sroll视图作为控件类型以使其工作..
答案 0 :(得分:1)
尝试这样可能对您有所帮助,但不确定,
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextField class]]) {
[yourtextfield resignFirstResponder];
}
}
要在scrollview上获取触摸事件,您必须进行geasture识别,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
[scroll addGestureRecognizer:singleTap];
-(void)singleTap{
[text resignFirstResponder];
//write whatever you want it.
}