1

I have a problem to close keyboard when I have more than 1 UITextField.

I have 2 TextField : TextField1 and TextField2 and I give each of them tag 1 and 2

I want to make if tag equal to 2 then it's hide the keyboard and if I touch textfield with tag equal to 1 the keyboard visible again, that's work with my code but when I turn it back the code not do like what I want, I touch TextField1 first then TextField2 the keyboard still visible and not hiding

My code is like this

func textFieldDidBeginEditing(textField: UITextField) {
        if (textField.tag == 2) {
            self.view.endEditing(false)
            textField.resignFirstResponder()
        } else {
            self.view.endEditing(true)
        }
    }

I also try to use Events like Editing Did Begin and Editing Changed but can't do like what I want

Please help me how to make keyboard hide when it's is already visible

Dirus
  • 1,033
  • 2
  • 14
  • 21
  • why are you resigning first responder or ending editing within a did begin editing delegate method? Won't that make it impossible to ever enter text? – jrturton May 20 '16 at 08:59
  • @jrturton I want when I touch my TextField2 it's still have blinking cursor but not showing the Keyboard – Dirus May 20 '16 at 09:05
  • Why is that? Is there some other method the user has for getting text into the text field? – jrturton May 20 '16 at 09:06
  • refer @simplebob answer. That is the solution of your problem. self.view.endEditing(false) is not going to dismiss the keyboard ever. – Wolverine May 20 '16 at 09:22

4 Answers4

2

Try this:

func textFieldDidBeginEditing(textField: UITextField) {
        if (textField.tag == 2) {
            self.view.endEditing(true)
        } else {
            textField.becomeFirstResponder()
        }
}

self.view.endEditing(true) will not show the keyboard, it actually will try to hide it forcing the first responder to resign.

Daniel
  • 20,420
  • 10
  • 92
  • 149
0

Try with below code.

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
     if(textField.tag == 2)
     {
          [[self.view viewWithTag:1] resignFirstResponder];
          return false;
     }
     else
     {
          return true;
     }
}
Apurv
  • 17,116
  • 8
  • 51
  • 67
0

You can also do it like using below textfield delegate method,

func textFieldShouldReturn(textField: UITextField) -> Bool
{
    if (textField == self.TextField1) {
        self.TextField2.becomeFirstResponder()
    }
    else {
        textField.resignFirstResponder()
        self.view.endEditing(true)
    }
    return true
}

Hope this will help you.

Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
0

I think that you didn't set UITextFieldDelgate on this class, So In the viewDidLoad()

  override func viewDidLoad() { 
         TextField1.delegate = self
         TextField2.delegate = self
    } 

Then add textFieldDidBeginEditing like this :-

   func textFieldDidBeginEditing(textField: UITextField) {
    if (textField.tag == 2) {
        self.view.endEditing(true)
    } else {
        textField.becomeFirstResponder()
    }

}

Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58