Objective c UitextField resignFirstResponder在滚动视图中不工作
我有两个函数要退出FirstResponder,但当文本字段在scrollview中时,这两个函数都不起作用 我的职能:Objective c UitextField resignFirstResponder在滚动视图中不工作,objective-c,cocoa-touch,uiscrollview,uitouch,Objective C,Cocoa Touch,Uiscrollview,Uitouch,我有两个函数要退出FirstResponder,但当文本字段在scrollview中时,这两个函数都不起作用 我的职能: -(BOOL)textFieldShouldReturn:(UITextField *)theTextField { if (theTextField == textField1) { [textField1 resignFirstResponder]; } if (theTextField == textField2) {
-(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 alltoucks]anyObject]
响应ANYObjects上的所有触摸
感谢您的帮助将继承
UIControl
的透明视图添加到它的最后面,大小等于您的滚动视图,然后为这个新视图创建iAction
,这不是更优雅一点吗?您还可以使用手势识别器来获取背景点击。使用cancelsTouchesInView=NO将所有其他触摸转发到右侧接收器
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTappedBackground:)];
tapGesture.cancelsTouchesInView = NO;
[self.scrollView addGestureRecognizer:tapGesture];
@界面滚动视图{
UITextField*_currentTF;
}
@实施
-(无效)viewDidLoad{
yourScrollView.delegate=self;
yourTextField.delegate=self;
}
#pragma标记UIScrollViewDelegate
-(无效)scrollView将开始刷新:(UIScrollView*)scrollView{
[_currentTF辞职第一响应者];
}
#pragma标记UITextFieldDelegate
-(BOOL)textField应该开始编辑:(UITextField*)textField{
_currentTF=文本字段;
返回YES;
}
你是说我的视图有scrollview,它有一个透明的新视图?假设你有scrollview。您应该通过Interface Builder在其上添加另一个UIView
。此视图应该完全类似于您的滚动视图框架,并且应该低于所有文本字段。然后将其类设置为UIControl
,并将您的touchsbegind
方法更改为某个iAction
方法,并使用此新方法将该新视图的简单绑定touchUpInside
——请参阅第4章的源代码。您能给我等效的swift代码吗?谢谢你最好对你的回答作些解释
@interface ScrollView <UIScrollViewDelegate,UITextFieldDelegate> {
UITextField *_currentTF;
}
@implementation
- (void)viewDidLoad {
yourScrollView.delegate = self;
yourTextField.delegate = self;
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[_currentTF resignFirstResponder];
}
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
_currentTF = textField;
return YES;
}