0
votes

I happening the following problem:

I have two TextField, one has the default behavior, and on the other, captures the textFieldDidBeginEditing method delegate, to present a UIViewController with PresentationStyle (Custom) and TransitionStyle (CrossDissolve). So far everything works fine. But if I edit the first textField (and leave the keyboard open) and then go to the second, the keyboard is open and I have no way to close it (as if the first textField had lost focus, not even the textFieldShouldReturn is called when I press the button intro).

I tried with:

  • TextFieldDidEndEditing (for calling resignFirstResponder)
  • In viewWillDissaper method (I also called resignFirstResponder)

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(textField: UITextField) {
        print("Resign Last TextField")
        textField.resignFirstResponder()
    }
    
    func textFieldDidBeginEditing(textField: UITextField) {
        print("Did Begin editing")
        if textField == self.departing || textField == self.returning{
            textField.resignFirstResponder()
            self.lastTextFieldSelected = textField
    
            let datePickerViewController = Util.getViewController("DatePickerViewController") as! DatePickerViewController
            datePickerViewController.dateStyle = NSDateFormatterStyle.ShortStyle
            datePickerViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
            datePickerViewController.modalPresentationStyle = UIModalPresentationStyle.Custom
            datePickerViewController.datePickerDelegate = self
    
            self.presentViewController(datePickerViewController, animated: true, completion: nil)
        }
    }
    

Edit: I try with self.view.endEditing(true) nothing.

I made a little example (something I check, is that textFieldShouldBeginEditing allows me to close the keyboard.): https://github.com/Abreu0101/TextFieldBug

1
if you want close keyboard at viewWillDisappear for any first responder you can try self.view.endEditing(false) if it still don't close you can try self.view.endEditing(true) , which will force close keyboard - HardikDG
In the situation you describe, is all your textFieldDidBeginEditing code actually executing? - matt
For dismiss keyboard. Try self.view.endEditing = true - tuledev
@matt, yes it's executing the code, but first run textFieldDidBeginEditing (in that step the first one, loose the focus), and when open the datePickerViewController (the keyboard not hide). Next, the delegate call textFieldDidEndEditing (but nothing happen). - José Roberto Abreu
@Pyro, anhtu, I will try that option (sometimes ago a tried that, but not in the viewWillDisapper). - José Roberto Abreu

1 Answers

0
votes

It looks like your textFieldDidEndEditing and the other methods are not even called. Make sure you set the UITextField's delegate:

textField.delegate = self

Also import the UITextFielDelegate in your class.

Hope that helps :)