Did anyone notice that UITextField
calls textFieldDidEndEditing
after clear button is pressed but text
property still has old data ?
I'm not sure what code-sample I can provide here. I'm using storyboard if that matters.
For now I have to rely on taking data from all edit controls on main form's "Submit" button. But ideally I'd prefer to collect data in textFieldDidEndEditing
handler.
Are there any better workarounds ?
I'm on iOS 6.
Update: Basically here is what I have on the form
UITextField
and UiButton
are on the form.resignFirstResponder
in handler of UITapGestureRecognizer
Steps to reproduce the issue:
textFieldDidEndEditing
is called. Property .text
has value I entered. All good.textFieldDidEndEditing
is called again. But property .text
still has value I just deleted !textFieldDidEndEditing
was never called.I'll upload sample project on GitHub tomorrow.
The text field calls various delegate methods during editing: Whenever the current text changes, it calls the textField (_:shouldChangeCharactersIn:replacementString:) method and posts the textDidChangeNotification notification. It calls the textFieldShouldClear (_:) method when the user taps the built-in button to clear the text.
It calls the textFieldShouldClear (_:) method when the user taps the built-in button to clear the text. It calls the textFieldShouldReturn (_:) method when the user taps the keyboard’s return button. Before resigning as first responder, the text field calls its delegate’s textFieldShouldEndEditing (_:) method.
Before becoming the first responder, the text field calls its delegate’s textFieldShouldBeginEditing (_:) method. Use that method to allow or prevent the editing of the text field’s contents. The text field becomes the first responder.
I ran into the exact same problem. In my case, at least, it was due to having added a UITapGestureRecognizer
to self.view
(to allow for dismissing the keyboard if tapping outside of a UITextField
) and setting cancelsTouchesInView=NO
on the gesture recognizer. I had set that property in order to get hyperlinking working on a TTTAttributesLabel
I have elsewhere in the View.
My workaround was to watch for keyboard show and hide notifications, and toggle that property accordingly:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShowNotification:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideNotification:) name:UIKeyboardDidHideNotification object:nil];
(sign up for notifications)
- (void)keyboardDidShowNotification:(NSNotification*)notification
{
tapGestureRecognizer.cancelsTouchesInView = YES;
}
- (void)keyboardDidHideNotification:(NSNotification *)notification
{
tapGestureRecognizer.cancelsTouchesInView = NO;
}
(handle notifications)
The only problem, behavior-wise, is that the hyperlink still doesn't work when the keyboard is displayed: touching it will simply dismiss the keyboard, not forward the touch to the link handler. But I can live with that. After the keyboard is dismissed, the link works fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With