XCode TextField隐藏键盘

时间:2016-01-13 09:50:23

标签: ios objective-c uitextfield

在我的申请中,我有TextFieldsUsernameNamePassword等。

因为我使用以下textfields年龄,性别和国家作为ActionSheet选民。我遇到的问题是当我键入password字段并移动到选择年龄时,键盘不会隐藏在第一次单击中。我必须在键盘隐藏后再次单击年龄字段。

任何人都可以帮我解决这个问题

- (BOOL)textFieldShouldBeginEditing:(LRTextField *)textField {
  NSLog(@"Tag %ld",(long)textField.tag);
  if (textField.tag == 3){
    return NO;
  } else if (textField.tag == 4){
    return NO;
  } else if (textField.tag==5){
    return NO;
  } else if (textField.tag==6){
    return NO;
  }
  return YES;
}

我正在使用LTTextField进行textfield验证。

4 个答案:

答案 0 :(得分:0)

按照以下步骤

在该pickerview文本字段上创建一个简单的按钮

添加操作

- (IBAction)hidekey:(id)sender {


    [self.view endEditing:YES];
    [_yourpickerview setHidden:NO];

}

此处所有文字字段resignFirstResponder

所以,当你继续前进textfield键盘将隐藏和选择视图显示

希望为你工作

答案 1 :(得分:0)

您可以使用resignFirstResponder

func textFieldShouldReturn(textField: UITextField) -> Bool {

    if textField.tag = passwordTag {
        textField.resignFirstResponder()        
    }
    return true
}

答案 2 :(得分:0)

我遇到了同样的问题,我制作了一小段代码,它们在每个iPhone的正确位置移动。 在viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

紧接着:

 - (void)keyboardDidShow: (NSNotification *) notif{
        CGSize keyboardsize = [[[notif userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.25];
        long moveScreen = ((long)(keyboardsize.height) - (long)(self.view.frame.size.height) + 465) * -1;
        if (moveScreen > 0)
            moveScreen = 0;
        self.view.frame = CGRectMake(rect.origin.x,moveScreen,rect.size.width,rect.size.height);
        [UIView commitAnimations];
    }

    - (void)keyboardDidHide: (NSNotification *) notif{
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.25];
        self.view.frame = rect;
        [UIView commitAnimations];
    }

答案 3 :(得分:0)

      func textFieldShouldReturn(textField: UITextField) -> Bool {

                if textField == self.emailField {
                  self.usernameField.becomeFirstResponder()
                } 
                else if textField == self.usernameField {
                  self.nameField.becomeFirstResponder()
                }
                else if textField == self.nameField {
                  self.passwordField.becomeFirstResponder()
                }

                else if textField == self.passwordField {
                  self.ageField.becomeFirstResponder()
                  self.ageField.resignFirstResponder();
                }

                return true;
              }

让ageTextField成为第一个响应者,然后让第一个响应者辞职