0

I am trying to understand delegate methods in general, and specifically how to dismiss a UIDatePicker that popovers from a text field.

According to the documentation, textFeildShouldBeginEditing returns true 'if an editing session should be initiated; otherwise, false to disallow editing.'

Why would I then tell the app to resignFirstResponder, which is meant to hide the keyboard / date picker (as in several examples on stackoverflow and noobie tutorials)?

What I don't understand is: if it should begin editing, why then hide the input devise? Obviously, I am misunderstanding one or both concepts.

func resign() {        
      dobTextField.resignFirstResponder()
      nameTextField.resignFirstResponder()
      println("resign gets printed, but the date picker is still visible!?!")
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {  
    if (textField === dobTextField) {
    resign()          // but should begin editing, oder?!?
}
Community
  • 1
  • 1
DrWhat
  • 2,360
  • 5
  • 19
  • 33
  • It depends, normally you should not resign from `ShouldBegin`, there is another delegate `shouldreturn` which gets called if user presses `return key` of the keyboard, you should normally resign there. Now sometimes user doesn't press return key and directly jump to another textfield, which changes the responder to next textfield. In your case you are trying to resign all textfield which might have keyboard, but it should exclude the current textfield, not all. – iphonic Nov 07 '14 at 08:58

2 Answers2

0

you should resign only the textfield not affected:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {  
    if textField == dobTextField {
        nameTextField.resignFirstResponder()
    } else if textField == nameTextField {
        dobTextField.resignFirstResponder()
    } 
    return true
}

this way you are resiging first responder only on the textfields that should not currently be edited. this helps if for some reason you accidentally have 2 textfields (or more) assigned first responder causing conflicts with multiple keyboards/datepickers and such.

Sebastian Flückiger
  • 5,525
  • 8
  • 33
  • 69
0

In the examples you cite, the textField is being used to display a date. When the user selects this field, the app designers want the UIDatePicker to be displayed instead of the keyboard. Hence they call resignFirstResponder to hide the keyboard. At the same time, they display the date picker.

ResignFirstResponder will not hide the date picker, so the "input device" (for this field) will still be available.

Also, note that in one case the developer has used textFieldShouldBeginEditing, and returns false because they are providing the date picker. In the other case the developer uses textFieldDidBeginEditing (which has no return value).

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • Thanks! I see the logic in that, and the self.dismissViewControllerAnimated method used in another IBAction call. – DrWhat Nov 10 '14 at 08:55