UITextField calls textFieldDidEndEditing on cleanup, but the `text` property has data

Has anyone noticed that a UITextField calls textFieldDidEndEditing after clicking the clear button, but the text property has old data?

I'm not sure which sample code I can provide here. I use a storyboard if that matters.

Currently, I have to rely on getting data from all the edit controls on the main Submit form. But ideally, I would prefer to collect data in the textFieldDidEndEditing handler.

Are there any better workarounds?

I am on iOS 6.

Update : basically this is what I have in the form

  • UITextField and UiButton are in form.
  • The keyboard has been replaced by a call to resignFirstResponder in the UITapGestureRecognizer handler

Steps to reproduce the problem:

  • Click the edit button. Enter the text.
  • Click out of text control.
  • textFieldDidEndEditing . The .text property has the entered value. All is well.
  • Click the edit control again.
  • Press the clear button.
  • textFieldDidEndEditing is called again. But the .text property still has a value that I just deleted!
  • Now that you see the cursor blinking inside a UITextField, click on the button on the form.
  • Keyboard rejected textFieldDidEndEditing has never been called.

Tomorrow I will post a sample project on GitHub.

+4
source share
3 answers

I ran into the same problem. In my case, at least this has to do with adding a UITapGestureRecognizer to self.view (to allow the keyboard to turn off if you click outside of UITextField ) and set cancelsTouchesInView=NO in the gesture recognizer . I set this property to get the hyperlink working on the TTTAttributesLabel I have elsewhere in the view.

My workaround was to monitor the keyboard and hide notifications, and toggle this property accordingly:

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

(register for notifications)

 - (void)keyboardDidShowNotification:(NSNotification*)notification { tapGestureRecognizer.cancelsTouchesInView = YES; } - (void)keyboardDidHideNotification:(NSNotification *)notification { tapGestureRecognizer.cancelsTouchesInView = NO; } 

(recording notifications)

The only behavioral problem is that the hyperlink still does not work when the keyboard is displayed: touching it will simply cancel the keyboard, and not drag it to the link handler. But I can live with it. After the keyboard is rejected, the link works fine.

+5
source

The first UITextFieldDelegate check UITextFieldDelegate assigned or not, then

implement delegate textFieldShouldClear and write code here clear the textField

To do this, you need to set the clearButtonMode property,

 yourTextField.clearButtonMode = UITextFieldViewModeWhileEditing; yourTextField.delegate = self; 

Then run the delegate textFieldShouldClear

.h file

 @interface myViewController: UIViewController <UITextFieldDelegate>{ } 

.m file

 -(BOOL)textFieldShouldClear:(UITextField *)textField yourTextFeild.text = @""; return YES; } 
+1
source

try here:

 -(BOOL) textFieldShouldReturn:(UITextField*) textField 
-one
source

Source: https://habr.com/ru/post/1500866/


All Articles