I have a Verification ViewController
, I get 4 digit verification code by SMS and I need to enter those code to login, I have created the ViewController
like this
As you can see four UITextField
s, I need to allow only single digit for each UITextField
,
What I tried: I was trying to use shouldChangeCharactersInRange:method:
, but its not getting called, I don't know what's wrong, I think because UITextField
s are in UITableView
so it is not working.
I was working on a similar functionality and did it in my way. Solution below. Swift 4
It can be achieve using UITextField delegate & by setting Tag for each Textfield in increasing order (say 1 - 4), below is the delegate handler to solve the issue.
Swift 4
Inspired by @Anurag Soni and @Varun Naharia answers
Variant A
Variant B (a little bit another behavior):
Also, I implemented this feature:
In the case where the last textFiled is empty, I just want to switch to the previous textFiled. I tried all this methods. But as for me the method below more elegant and works like a charm:
Variant A
Variant B (a little bit another behavior):
Assign
EnterConfirmationCodeTextField
for each of your textFields and set they appropriatetag
value.Provide the tag to the textfield like 1,2,3,4 and directly use it
use this code if you don't want work with tag and it works better then above
Swift 4
when you input a number from the pin code into a text field, you should let a number be shown and then the next text field will become the first responder, so change the first responder after the code was in the text view
for more details and the simple demo, see this link