我有一个带有一些UITextField的UIView,但是当我点击它们时,我无法隐藏键盘并且“发送”按钮位于键盘后面:
我怎么能隐藏它?
谢谢!
答案 0 :(得分:4)
在你的代码中添加UITextField的委托方法。不要忘记设置委托。每当返回键是按下键盘时,将调用下面的方法。同时设置其代表:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
编辑:当一旦专注于textField并专注于其他文本字段时,该文本字段将不会重新签名,所以这样做,因为我假设从最顶层你有textField1,textField2,textField3,textField4如果还有.. ..添加此委托方法。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == textField1) //motive resign other textFields
{
[textField2 resignFirstResponder];
[textField3 resignFirstResponder];
[textField4 resignFirstResponder];
}
else if(textField == textField2) //motive resign other textFields
{
[textField1 resignFirstResponder];
[textField3 resignFirstResponder];
[textField4 resignFirstResponder];
}
else if(textField == textField3) //motive resign other textFields
{
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
[textField4 resignFirstResponder];
}
else if(textField == textField4) //motive resign other textFields
{
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
[textField3 resignFirstResponder];
}
return YES;
}
编辑你也可以使用它:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES];
return YES;
}
答案 1 :(得分:0)
我使用此代码,当您点击视图中的任意位置时,此事件会运行,并且您不依赖于textFields事件:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([textField1 isFirstResponder] && [touch view] != textField1) {
[textField1 resignFirstResponder];
}
if ([textField2 isFirstResponder] && [touch view] != textField2) {
[textField2 resignFirstResponder];
}
...
[super touchesBegan:touches withEvent:event];
}