UITextField Clear Button Calls didEndEditing Twice - ios

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.

Related

First Responder Of Two Textfields

So in a small app I am making, if the user clicks the sign in button, two text fields pop up from the bottom of the screen(the username and the password textfields). Now I want the keyboard to pop up automatically as well when the user clicks on the sign in button. To do this I said:
self.userNameTextField.becomeFirstResponder()
self.passwordTextField.becomeFirstResponder()
Now for some reason I can't click on the userNameTextField. The cursor is always stuck on the passwordTextField. I think this is because it is making the password text field the first responder so it won't let me click on the username one. Is there any other way to automatically bring up the keyboard without using this firstResponder method?
by doing what you described here you are making userNameTextField a first responder, and right after that you are making passwordTextField a first responder - which ends up having passwordTextField set as first responder (because Swift is lightning fast and you can't even notice the moment when userNameTextField becomes a first responder).
So, pick which one will you call (probably self.userNameTextField.becomeFirstResponder() because you want user to type the username first) and then you could call resignFirstResponder() (which will resign userNameTextField from being a first responder when user taps the "Return" on the keyboard) by implementing UITextFieldDelegate method textFieldShouldReturn (let me know if you need some help there) and right after that call self.passwordTextField.becomeFirstResponder() to make your passwordTextField a first responder so that user can type a password.

Distinguishing between EditingDidBegin because of user action vs programatic setting

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.

Prevent iOS keyboard from disappearing / reappearing between UITextFields?

I've got a form with some UITextField instances, and I've set up an accessory view to flip back and forth between them as you fill in the form. The ugliness is that the keyboard slides away, then immediately slides back for the next form.
Since it's going to remain there, is there a way to get it to simply stay up throughout the whole form, rather than this ugly/silly gone/back behaviour? I may be doing something wrong programmatically, I'm telling fields to resignFirstResponder before the new one does becomeFirstResponder – is that the cause?
You don't need to called resignFirstResponder when switching between text fields, iOS will handle this for you when calling becomeFirstResponder on another object. You just need to call resignFirstResponder if/when you want to hide the keyboard, say at the end of the form.
Yes, that is the cause. You can just call becomeFirstResponder without calling resignFirstResponder and you'll get what you want
When you select other UITextField than automatically the last UITextField resign first responder so you should not resignFirstResponder every time. Just resign when you done with UITextField or user click on the UIView.

Keep keyboard always on top & visible

I have view with a text field, an image and a few buttons.
I want to make sure the keyboard is displayed and is on top when the view is displayed
AND
I want to make sure it doesn't go away after I type something in to the text field and submit it.
I called [txtField becomeFirstResponder] with viewdidload and the keyboard is appearing by default but with a tiny delay after the view is displayed.
Also the becomefirstresponder doesn't help after I have my text field submitted.
Thanks in advance for your help!
Also the becomefirstresponder doesn't help after I have my text field submitted.
That part makes no sense. By default, a text field does not dismiss the keyboard unless you dismiss it with endEditing: or resignFirstResponder. If the keyboard is going away, you must be making it go away. So don't and it won't.
EDIT: And indeed, your comment later reveals the answer: you've hooked up the didEndOnExit control event from the text field. Well, that causes the keyboard to be dismissed when the user presses the Done button! So you are effectively hitting yourself in the face and then complaining that someone is hitting you in the face.
So the solution, obviously, is don't hook up the didEndOnExit control event (to anything). Instead, just give the text field a delegate and use the delegate messages to learn what the user is doing. None of those have any automatic behavior with regard to the keyboard, so the keyboard won't be dismissed automatically. For example, to learn when the user is typing, use textField:shouldChangeCharactersInRange:replacementString:. To learn when the user has hit the Done button, use textFieldShouldReturn:. And so on.

iOS keyboard flickers when switching view controllers

I have a registration form and I want to have the keyboard always on top.
The way I'm doing it now, is that when the user moves between view controllers, in viewDidLoad, the first UITextField becomes the first responder.
The problem is that the keyboard flickers (disappears and then appears again) when the user moves between view controllers.
Also, related to this: I have a form with a few uitextfields. When the user presses next it goes to the next uitextfield using becomefirstresponder. When the user is in the last textfield, the keyboard button becomes "Done". Then, when the user presses it, if there's an error with the last field, it should get the focus (calls becomeFirstResponder) but that doesn't happen (nothing get's the focus and the keyboard goes down). All the other fields get the focus fine, just this last field doesn't. I've tried about everything: switching to other textfields and back. The problem is that done automatically removes the keyboard.
You should have made two separate questions for this.
First, your flickering:
I'm guessing you're using a UINavigationController. You can add an invisible UITextField somewhere in the UINavigationController, which you give focus before you switch to a new ViewController. Then, when the new ViewController has appeared (viewDidAppear), set the focus to the first textField as you want.
However, the entire approach is kind of hackey and I don't recommend you use it. Instead, try using several views in a scrollView, of which you change the offset when you move to the new view. This will also solve the flickering.
Second, losing firstResponder status on Done:
The done button is specifically there to indicate exactly that which it says; Done. Pressing this assumes the user is finished and that no text is left to type, thus dismissing the keyboard.
If you really want to keep the Done button, then try the following;
Allow the user to dismiss the keyboard.
Upon dismissal, check for the error in the last field.
If there is an error, instead of calling [lastField becomeFirstResponder], try [self performSelector:#selector(thisSelectorWillCallFirstResponder) withObject:nil afterDelay:1.0].
In the method thisSelectorWillCallFirstResponder call [lastField becomeFirstResponder].
This will give time for the keyboard to disappear, before making it pop up again, so it doesn't interfere with the becomeFirstResponder call.
Another method would be to not use a Done button, but instead use the return key. You can intercept return anytime with the delegate method textFieldShouldReturn:. There you can handle any error checking, without causing the textField to lose its focus.

Resources