在iOS中使用resignFirstResponder时,键盘不会消失

时间:2017-03-30 03:45:38

标签: ios objective-c

编码环境sierra 10.12,Xcode 8.1。两个TextField我roorView,如果第一个文本框' s的文字是零,第二个文本框将可' T为editing.When我用resignFirstResponder方法来关闭键盘textFieldDidBeginEditing:方法,键盘不会消失。我在rootView中添加了一个全屏TapGesture。我很困惑,有没有人有想法帮我解决这个问题?

`

  #pragma mark -- UITextFieldDelegate
        - (void)textFieldDidBeginEditing:(UITextField *)textField{
            if (textField == self.password) {
                [self.username resignFirstResponder];
                NSString *name = self.username.text;
                if ([name isEqualToString:@""]) {
                    [CFBlurHUD showFaild:@"sign in error!"];
                    [self performSelector:@selector(dismmiss) withObject:nil afterDelay:1.5f];
                    self.password.enabled = NO;
                }
            }
        }
        - (void)dismmiss{
            [CFBlurHUD dismiss];
            self.password.enabled = YES;
        }

`

3 个答案:

答案 0 :(得分:0)

因为当前的textfiled是密码:if (textField == self.password) {。但你设置:

[self.username resignFirstResponder];

这不正确。 尝试:

[self.username resignFirstResponder];
  [self.password resignFirstResponder];

或:

self.view.endEditting = true;
  

使用您的UITextFieldDelegate方法,特别是UITextFieldShouldBeginEditing并返回NO并执行代码以显示弹出窗口。这样,键盘永远不会以

开头显示
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
 [self.view endEditing:YES]; 

  return NO;
 }

答案 1 :(得分:0)

如果您想简单地从视图中解除键盘,请尝试以下操作:

self.view.endEditing = true;
  

使用您的UITextFieldDelegate方法,特别是UITextFieldShouldBeginEditing并返回NO并执行代码以显示弹出窗口。这样,键盘永远不会以

开头显示
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
 [self.view endEditing:YES]; 

  return NO;
 }

答案 2 :(得分:0)

解除键盘的第一步在storyboard中提供textfeild委托

Select textFeild drag into view controller

Give textFeild delegate

或者也可以通过代码提供:

- (void)dismmiss{
    [self.view endEditing:YES];
    [CFBlurHUD dismiss];
    self.password.enabled = YES;
}

点击手势方法时忽略键盘:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

或同时在视图中的任何位置触摸时关闭:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

或者当用户按下键盘上的返回按钮时:

{{1}}