Distinguishing between EditingDidBegin because of user action vs programatic setting - ios

When I set the text of a UITextField programmatically, the EditingChanged action is not sent to my view controller, the action is only sent when the user changes the text through the keyboard.
However, when I call becomeFirstResponder() on a text field, it does send the EditingDidBegin action. So I can't tell if editing began because the user tapped on the UITextField, or because I did it programmatically. Do you know how I can easily distinguish between the two?
I'm looking for a way to know when the user taps in a UITextField and only when the user taps on it, not when becomeFirstResponder is called on it. Do you know of one?

when I call becomeFirstResponder()
The secret is that when you call becomeFirstResponder(), you call it. Your code is running. You are in control. You can do whatever you want.
So if you need your EditingChanged handler to know that you called becomeFirstResponder(), you simply have to make a notation of this fact beforehand. For example, you could have a Bool property justCalledBecomeFirstResponder which you set to true just before calling becomeFirstResponder(). Now your EditingChanged handler has only to examine (and possibly reset to false) this Bool property before proceeding.

Related

Executing a method in a dart TextField on every key press

I have a TextField in my project, which will be used to act as an autocomplete text field. I need it to respond to key presses.
After researching on Flutter's documentation, https://flutter.io/docs/cookbook/forms/text-field-changes, I found two ways of allowing a TextField to execute a method every time the TextField's text changed, however it only executes that method when you hit and enter and leave the focus of the TextField
I have used both the controller method, and the onChanged method and neither work.
Is there a way of having a TextField respond to every key press, as you type in a word?
OnChanged should work fine. If you are trying to change something on the screen you will have to make a call to setState() and your functionality with in that call.

ios/xcode/objective c: Capture last keystroke in textfield

I have a method to enable the save button that I want to fire when the user meets several conditions, for example, username is at least 4 characters. To test these conditions I need to check with every keystroke. What is best touch event (through storyboard) to capture this?
Have tried "Editing did end" however, that doesn't fire until you click out of textfield. I'd rather see button enabled as soon as textfield has acceptable entry. Also tried on touchup inside and value changed but neither seem to work.
What is best touch event so that as soon as user types fourth character, button is enabled?
Here is action method that textfield is wired to. Note it does not say anything about touch event which is present in storyboard.
- (IBAction)changedText:(id)sender {
[self updateSaveButton];
-(void) updateSaveButton
{
NSString *name = self.username.text;
NSLog(#"update save button called");
self.submitButton.enabled = (name.length > 3);
}
What I do is to assign the text field a delegate. In this case, the delegate would whatever is the instance whose class's code you have shown in your question.
The delegate is sent a bunch of delegate messages, documented in UITextFieldDelegate. In this case, the one I would use is textField:shouldChangeCharactersInRange:replacementString:, which fires every time the user does anything that proposed to change the text in the field.
Simply respond as you see fit, and return YES to allow the change to take place.
Alternatively, you can treat the text field as a control (UIControl) and use the text field's Editing Changed control event to set up a target-action pair. But in actual practice I always end up using the delegate method instead.

resignFirstResponder vs. endEditing for Keyboard Dismissal

In Swift, both [someTextField].resignFirstResponder() and self.view.endEditing(true) accomplish the same task - hiding the keyboard from the user's view and de-focusing whatever text field was using it. I understand that the former is specific to a particular field, while the latter encompasses the entire view, but other than wanting to target a specific text field, when is one preferred/recommended over the other?
someTextField.resignFirstResponder()
resignFirstResponder() is good to use any time you know exactly which text field is the first responder and you want to resign its first responder status. This can be slightly more efficient then the alternative, but if you're doing something such as creating a custom control, this can make plenty of sense. Perhaps you have a text field, and when the "Next" button is pressed, you want to get rid of the keyboard and present a date picker, for example. Here, I would definitely use resignFirstResponder()
self.view.endEditing(true)
I typically reserve this for scenarios when I just absolutely need to clear the keyboard no matter what is currently going on, for whatever reason. Perhaps, I've got a slide-over menu? Just before this slides out, no matter what is going on, the keyboard should go away, so I'll make sure everything resigns its first responder status. It's important to note that endEditing() will look through the entire hierarchy of subviews and make sure whatever is the first responder resigns its status. This makes it less efficient then calling resignFirstResponder() if you already have a concrete reference to the first responder, but if not, it's easier than finding that view and having it resign.
There is no such strict rule.
You use resignFirstResponder when you have the reference of the text field currently holding first responder status. When you don't have the reference or you are unsure about that, endEditing would do the trick.
One thing however should be noted that endEditing has a boolean parameter that we occasionally set to true. By setting this parameter to true the view, on which endEditing has been called, will force every child text field to resign first responder status irrespective of it has returned a false value from textFieldShouldEndEditing delegate method. On the contrary calling endEditing with false would only ask (not force) the text field to resign, respecting the return value from textFieldShouldEndEditing protocol method.

UITextField Clear Button Calls didEndEditing Twice

I have a UITextField with a Clear button. Whenever the user resigns first responder on the textfield, it submits the change to a webservice.
When the user taps the clear button, seemingly, the textfield loses its first responder status, causing the didEndEditing delegate method to be called. So the webservice call is made, with the old, populated value. Then, the textfield is cleared, calling didEndEditing again, calling the webservice again.
Why does this happen this way? I would expect the clear button to empty the text and then call didEndEditing on the delegate so you only get one message.
If you're referring to the built in clear button on the UITextField, when the user taps it that causes the text field to loose focus as the button itself receives the touch - this triggers the first -didEndEditing. Then the release of the clear button causes it to clear the text field, which calls -didEndEditing to fire yet again when the text is actually cleared. Yeah, it is definitely less than ideal.

UITextField Custom InputView - How do I get textFieldShouldReturn to fire?

I have created a custom inputView and have a Done button. Does anyone know how I can wire the done button up to work the same as a return key on the built in keyboard.
I basically need to get the textFieldShouldReturn method to fire for a UITextField.
I know I can call resignFirstResponder on the textfield, but this does not fire the textFieldShouldReturn method.
If it's a custom input view, and a custom "Done" button, why do you need to use the text field delegate methods? You can wire the done button to your own action.
If you have some common code in your textFieldShouldReturn method this could be moved out to another method which can be called from the delegate method and your new action.

Resources