First Responder Of Two Textfields - ios

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.

Related

Dismiss keyboard when Password Autofill suggestion is selected

I have a loginViewController with Password Autofill feature. Everything works as it should except one small detail, which I cannot find a solution to. My conclusion is that there is none, but perhaps I could be wrong.
When the user selects a stored username and password, it autofills. But the keyboard stays open, and the user must dismiss the keyboard and press the log in button.
The documentation Password autofill workflow states that the delegate function:
textField:shouldChangeCharactersInRange:replacementString:
is called, when an quickbar item is pressed. I would like to close the keyboard so that the user can immediately press the log in button, and proceed, after a password is selected. But in this delegate function its not possible since its called whenever the textfields are modified as well.
Is there any solution to do this?

How to jump to the next flield when "return" is taped in swift?

How i configure cursor to jump to the next field (description) when i tap "return" on keyboard in swift?
when I finish to choose the name on field and tap "return" on keyboard
the cursor automatically jump to the "description" field... How i can do this?
app screen
I tried resolve this problem with this answer but unsuccessful (link:Using "Next" as a Return Key)
You can respond to changes in the UITextFields or UILabels by setting up outlets to the textfield actions didEndOnExit. Then in your implementation of that action outlet you can make your other UITextField active with
textField.becomeFirstResponder()
I was able to make this exact situation work by creating an action outlet editingDidEnd to the textfield. So, in my situation, I'm moving from firstNameTextField to lastNameTextField when user hits return.
#IBAction func firstNameEditingDidEnd(sender: UITextField) {
lastNameTextField.becomeFirstResponder()
}

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.

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.

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