我有 2 个函数用于 resignFirstResponder 但当文本字段位于滚动视图中时它们都不起作用
我的职能:
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == textField1) {
[textField1 resignFirstResponder];
}
if (theTextField == textField2) {
[textField2 resignFirstResponder];
}
if (theTextField == textField3) {
[textField3 resignFirstResponder];
}
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
[textField3 resignFirstResponder];
}
我已经在 IB 中链接了滚动视图,我无法找出为什么只有当我单击滚动视图之外时它才不起作用。所以它只响应视图,但为什么?我认为[[event allTouches] anyObject]
响应 ANYObjects 上的 ALLtouches
感谢帮助
您还可以使用手势识别器来获取背景点击。使用cancelsTouchesInView = NO 将所有其他触摸转发到正确的接收器。
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTappedBackground:)];
tapGesture.cancelsTouchesInView = NO;
[self.scrollView addGestureRecognizer:tapGesture];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)