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.
Try this sample tutorial passcode lock
ViewController.h
ViewController.m
You can change the text field like this by using the delegate function of the text field. Initially, you need to set the delegate and the tag of each text field.
Swift 2
Swift 4
I have taken one Hidden text field & four imageViews for that with two images. One for Blank and other for Bullet same as iOS default.
Also set tags for four imageviews.
On Load set Focus for Pin Code
Then change imageview's images as per user input and only allow four characters
After Four characters call function for generated PIN Code and store it in User Defaults same as iOS default PIN settings
Here, you can again call StartPinCode method for re-confirming code.
Hopefully, it'll help you.
Thanks
Modified Anurag Soni's answer in Swift 3.
textFields
and the text fields have ordered tags setIt prevents from pasting more than one digit
try this : - For swift 3.0
converted Anurag Soni answer in swift 3.0
You just have to implement this method only.
Just use
TextFieldDelegate
method and check the length of the TextField after every changes