Allowing single digit in UITextField in iOS









up vote
12
down vote

favorite
7












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 UITextFields, 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 UITextFields are in UITableView so it is not working.










share|improve this question























  • Did you set the viewController as the textField's delegate? The delegate methods won't be called otherwise.
    – Jsdodgers
    Sep 10 '13 at 5:49










  • this is not because u use txtfld in table. and one more thing i want to know that are you testing on iOS7-beta or lower version
    – Anurag Soni
    Sep 10 '13 at 5:51










  • Yes I have set the delegate and I can see the values when i click on submit..
    – krish
    Sep 10 '13 at 5:52










  • @AnuragSoni I am using IOS 6..
    – krish
    Sep 10 '13 at 5:53










  • try to handle logic in this method- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    – Tendulkar
    Sep 10 '13 at 5:57














up vote
12
down vote

favorite
7












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 UITextFields, 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 UITextFields are in UITableView so it is not working.










share|improve this question























  • Did you set the viewController as the textField's delegate? The delegate methods won't be called otherwise.
    – Jsdodgers
    Sep 10 '13 at 5:49










  • this is not because u use txtfld in table. and one more thing i want to know that are you testing on iOS7-beta or lower version
    – Anurag Soni
    Sep 10 '13 at 5:51










  • Yes I have set the delegate and I can see the values when i click on submit..
    – krish
    Sep 10 '13 at 5:52










  • @AnuragSoni I am using IOS 6..
    – krish
    Sep 10 '13 at 5:53










  • try to handle logic in this method- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    – Tendulkar
    Sep 10 '13 at 5:57












up vote
12
down vote

favorite
7









up vote
12
down vote

favorite
7






7





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 UITextFields, 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 UITextFields are in UITableView so it is not working.










share|improve this question















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 UITextFields, 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 UITextFields are in UITableView so it is not working.







ios objective-c swift uitableview uitextfield






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 26 '17 at 15:45









Cœur

16.9k9102139




16.9k9102139










asked Sep 10 '13 at 5:45









krish

3012626




3012626











  • Did you set the viewController as the textField's delegate? The delegate methods won't be called otherwise.
    – Jsdodgers
    Sep 10 '13 at 5:49










  • this is not because u use txtfld in table. and one more thing i want to know that are you testing on iOS7-beta or lower version
    – Anurag Soni
    Sep 10 '13 at 5:51










  • Yes I have set the delegate and I can see the values when i click on submit..
    – krish
    Sep 10 '13 at 5:52










  • @AnuragSoni I am using IOS 6..
    – krish
    Sep 10 '13 at 5:53










  • try to handle logic in this method- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    – Tendulkar
    Sep 10 '13 at 5:57
















  • Did you set the viewController as the textField's delegate? The delegate methods won't be called otherwise.
    – Jsdodgers
    Sep 10 '13 at 5:49










  • this is not because u use txtfld in table. and one more thing i want to know that are you testing on iOS7-beta or lower version
    – Anurag Soni
    Sep 10 '13 at 5:51










  • Yes I have set the delegate and I can see the values when i click on submit..
    – krish
    Sep 10 '13 at 5:52










  • @AnuragSoni I am using IOS 6..
    – krish
    Sep 10 '13 at 5:53










  • try to handle logic in this method- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    – Tendulkar
    Sep 10 '13 at 5:57















Did you set the viewController as the textField's delegate? The delegate methods won't be called otherwise.
– Jsdodgers
Sep 10 '13 at 5:49




Did you set the viewController as the textField's delegate? The delegate methods won't be called otherwise.
– Jsdodgers
Sep 10 '13 at 5:49












this is not because u use txtfld in table. and one more thing i want to know that are you testing on iOS7-beta or lower version
– Anurag Soni
Sep 10 '13 at 5:51




this is not because u use txtfld in table. and one more thing i want to know that are you testing on iOS7-beta or lower version
– Anurag Soni
Sep 10 '13 at 5:51












Yes I have set the delegate and I can see the values when i click on submit..
– krish
Sep 10 '13 at 5:52




Yes I have set the delegate and I can see the values when i click on submit..
– krish
Sep 10 '13 at 5:52












@AnuragSoni I am using IOS 6..
– krish
Sep 10 '13 at 5:53




@AnuragSoni I am using IOS 6..
– krish
Sep 10 '13 at 5:53












try to handle logic in this method- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
– Tendulkar
Sep 10 '13 at 5:57




try to handle logic in this method- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
– Tendulkar
Sep 10 '13 at 5:57












13 Answers
13






active

oldest

votes

















up vote
17
down vote



accepted










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.



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

if ((textField.text.length >= 1) && (string.length > 0))

NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (! nextResponder)
nextResponder = [textField.superview viewWithTag:1];

if (nextResponder)
// Found next responder, so set it.
[nextResponder becomeFirstResponder];

return NO;

return YES;



Swift 2



func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0)
let nextTag = textField.tag + 1;

// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag);

if (nextResponder == nil)
nextResponder = textField.superview?.viewWithTag(1);

textField.text = string;
nextResponder?.becomeFirstResponder();
return false;

else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
// on deleting value from Textfield
let previousTag = textField.tag - 1;

// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);

if (previousResponder == nil)
previousResponder = textField.superview?.viewWithTag(1);

textField.text = "";
previousResponder?.becomeFirstResponder();
return false;

return true;



Swift 4



func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

if textField.text!.count < 1 && string.count > 0
let nextTag = textField.tag + 1

// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag)

if (nextResponder == nil)

nextResponder = textField.superview?.viewWithTag(1)

textField.text = string
nextResponder?.becomeFirstResponder()
return false

else if textField.text!.count >= 1 && string.count == 0
// on deleting value from Textfield
let previousTag = textField.tag - 1

// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag)

if (previousResponder == nil)
previousResponder = textField.superview?.viewWithTag(1)

textField.text = ""
previousResponder?.becomeFirstResponder()
return false

return true







share|improve this answer






















  • HI Anurag thanks for the code, this is working like charm, but when i enter the next code in textbox1 ,cursor is going to textbox2 but code is not getting updated , how to solve ?
    – krish
    Sep 10 '13 at 6:10











  • you want to enter one character in one textfield , am i getting right? so can you explain a little bit more what is your problem now
    – Anurag Soni
    Sep 10 '13 at 7:14











  • @AnuragSoni i want to move automatic in nextfield
    – Jigar
    Jun 6 '16 at 5:12

















up vote
7
down vote













use this code if you don't want work with tag and it works better then above



func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
// On inputing value to textfield
if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
if(textField == txtOne)
txtTwo.becomeFirstResponder()

if(textField == txtTwo)
txtThree.becomeFirstResponder()

if(textField == txtThree)
txtFour.becomeFirstResponder()

textField.text = string
return false

else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
// on deleting value from Textfield
if(textField == txtTwo)
txtOne.becomeFirstResponder()

if(textField == txtThree)
txtTwo.becomeFirstResponder()

if(textField == txtFour)
txtThree.becomeFirstResponder()

textField.text = ""
return false
else if ((textField.text?.characters.count)! >= 1 )
textField.text = string
return false

return true






share|improve this answer


















  • 1




    This is acceptable answer, thanks.
    – Nij
    Jul 12 '17 at 10:32

















up vote
3
down vote













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.



func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0)
let nextTag = textField.tag + 1;

// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag);

if (nextResponder == nil)
nextResponder = textField.superview?.viewWithTag(1);

textField.text = string;
nextResponder?.becomeFirstResponder();
return false;

else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
// on deleteing value from Textfield
let previousTag = textField.tag - 1;

// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);

if (previousResponder == nil)
previousResponder = textField.superview?.viewWithTag(1);

textField.text = "";
previousResponder?.becomeFirstResponder();
return false;

return true;






share|improve this answer



























    up vote
    2
    down vote













    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



    - (void)startPinCode

    txtPinCodeLockDigits.text = @"";

    for (int i = 1; i <= 4; i++)

    UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
    [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


    [txtPinCodeLockDigits becomeFirstResponder];



    Then change imageview's images as per user input and only allow four characters



    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
    textField.text = result;

    for (int i = 1; i <= 4; i++)

    UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
    if (i <= [result length])
    [img setImage:[UIImage imageNamed:@"Img_BG_PinCode_Filled.png"]];
    else
    [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


    NSLog(@"Result :: %@", result);

    if ([result length] == 4)
    [self performSelector:@selector(keyGenerationForApplication:) withObject:result afterDelay:0.2];


    return NO;



    After Four characters call function for generated PIN Code and store it in User Defaults same as iOS default PIN settings



    - (void)keyGenerationForApplication:(NSString *)pinCode

    int appCode = [pinCode intValue];
    [DefaultsValues setIntegerValueToUserDefaults:appCode ForKey:PIN_LOCK_PATTERN];



    Here, you can again call StartPinCode method for re-confirming code.



    Hopefully, it'll help you.

    Thanks






    share|improve this answer



























      up vote
      2
      down vote













      swift 2.3



       class BankDepositsWithOTPVC: UIViewController 

      let limitLength = 1

      override func viewDidLoad()
      super.viewDidLoad()





      // MARK: Textfield Validator

      extension BankDepositsWithOTPVC : UITextFieldDelegate


      func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
      // On inputing value to textfield
      if (textField.text?.characters.count < 1 && string.characters.count > 0)
      let nextTag = textField.tag + 1;

      // get next responder
      let nextResponder = textField.superview?.viewWithTag(nextTag);

      if (nextResponder == nil)
      textField.resignFirstResponder()
      // nextResponder = textField.superview?.viewWithTag(1);

      textField.text = string;
      nextResponder?.becomeFirstResponder();
      return false;

      else if (textField.text?.characters.count >= 1 && string.characters.count > 0)
      // maximum 1 digit

      textField.text = "";

      let nextTag = textField.tag + 1;

      // get next responder
      let nextResponder = textField.superview?.viewWithTag(nextTag);

      if (nextResponder == nil)
      textField.resignFirstResponder()
      // nextResponder = textField.superview?.viewWithTag(1);

      textField.text = string;
      nextResponder?.becomeFirstResponder();
      return false;

      else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
      // on deleteing value from Textfield
      let previousTag = textField.tag - 1;

      // get next responder
      var previousResponder = textField.superview?.viewWithTag(previousTag);

      if (previousResponder == nil)
      previousResponder = textField.superview?.viewWithTag(1);

      textField.text = "";
      previousResponder?.becomeFirstResponder();
      return false;

      //return true;

      guard let text = textField.text else return true
      let newLength = text.characters.count + string.characters.count - range.length
      return newLength <= limitLength






      Objective-C



      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

      if ((textField.text.length < 1) && (string.length > 0))


      NSInteger nextTag = textField.tag + 1;
      UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
      if (! nextResponder)
      [textField resignFirstResponder];

      textField.text = string;
      if (nextResponder)
      [nextResponder becomeFirstResponder];

      return NO;

      else if ((textField.text.length >= 1) && (string.length > 0))
      //FOR MAXIMUM 1 TEXT

      NSInteger nextTag = textField.tag + 1;
      UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
      if (! nextResponder)
      [textField resignFirstResponder];

      textField.text = string;
      if (nextResponder)
      [nextResponder becomeFirstResponder];

      return NO;

      else if ((textField.text.length >= 1) && (string.length == 0))
      // on deleteing value from Textfield

      NSInteger prevTag = textField.tag - 1;
      // Try to find prev responder
      UIResponder* prevResponder = [textField.superview viewWithTag:prevTag];
      if (! prevResponder)
      [textField resignFirstResponder];

      textField.text = string;
      if (prevResponder)
      // Found next responder, so set it.
      [prevResponder becomeFirstResponder];

      return NO;


      return YES;






      share|improve this answer





























        up vote
        2
        down vote













        Provide the tag to the textfield like 1,2,3,4 and directly use it



        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool


        let next:NSInteger

        if string == ""

        next = textField.tag - 1;


        else

        next = textField.tag + 1;



        if (textField.text?.characters.count)! >= 1

        if textField.tag == 4

        if string == ""

        textField.text = ""

        let temptf = self.view.viewWithTag(next) as! UITextField

        temptf.becomeFirstResponder()

        return false


        else

        if (textField.text?.characters.count)! > 1

        let stringg = textField.text!
        textField.text = stringg.replacingOccurrences(of: stringg, with: string)

        return false


        else
        if string == ""

        textField.text = ""

        if next != 0

        let temptf = self.view.viewWithTag(next) as! UITextField
        temptf.becomeFirstResponder()



        return false


        else

        if (textField.text?.characters.count)! > 1


        let stringg = textField.text!
        textField.text = stringg.replacingOccurrences(of: stringg, with: string)



        let temptf = self.view.viewWithTag(next) as! UITextField

        temptf.becomeFirstResponder()





        return true

        }





        share|improve this answer



























          up vote
          1
          down vote













          Try this
          sample tutorial passcode lock



          ViewController.h



          #import <UIKit/UIKit.h>
          @interface ViewController : UIViewController<UITextFieldDelegate>

          IBOutlet UITextField *txtPassword;

          @end


          ViewController.m



          - (void)viewDidLoad

          [super viewDidLoad];
          txtPassword.delegate=self;



          - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

          NSUInteger newLength = [textField.text length] + [string length] - range.length;
          return (newLength > 1) ? NO : YES;






          share|improve this answer





























            up vote
            1
            down vote













            Swift 4



            Inspired by @Anurag Soni and @Varun Naharia answers



            Variant A



            extension EnterConfirmationCodeTextField: UITextFieldDelegate 

            func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
            guard let textFieldCount = textField.text?.count else return false

            // Сlosure
            let setValueAndMoveForward =
            textField.text = string
            let nextTag = textField.tag + 1
            if let nextResponder = textField.superview?.viewWithTag(nextTag)
            nextResponder.becomeFirstResponder()



            // Сlosure
            let clearValueAndMoveBack =
            textField.text = ""
            let previousTag = textField.tag - 1
            if let previousResponder = textField.superview?.viewWithTag(previousTag)
            previousResponder.becomeFirstResponder()



            if textFieldCount < 1 && string.count > 0

            setValueAndMoveForward()

            if textField.tag == 4
            print("Do something")


            return false

            else if textFieldCount >= 1 && string.count == 0

            clearValueAndMoveBack()
            return false

            else if textFieldCount >= 1 && string.count > 0

            let nextTag = self.tag + 1
            if let previousResponder = self.superview?.viewWithTag(nextTag)
            previousResponder.becomeFirstResponder()

            if let activeTextField = previousResponder as? UITextField
            activeTextField.text = string



            return false


            return true






            Variant B (a little bit another behavior):



            extension EnterConfirmationCodeTextField: UITextFieldDelegate 

            func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
            guard let textFieldCount = textField.text?.count else return false

            // Сlosure
            let setValueAndMoveForward =
            textField.text = string
            let nextTag = textField.tag + 1
            if let nextResponder = textField.superview?.viewWithTag(nextTag)
            nextResponder.becomeFirstResponder()



            // Сlosure
            let clearValueAndMoveBack =
            textField.text = ""
            let previousTag = textField.tag - 1
            if let previousResponder = textField.superview?.viewWithTag(previousTag)
            previousResponder.becomeFirstResponder()



            if textFieldCount < 1 && string.count > 0

            setValueAndMoveForward()

            if textField.tag == 4
            print("Do something")


            return false

            else if textFieldCount >= 1 && string.count == 0

            clearValueAndMoveBack()
            return false

            else if textFieldCount >= 1

            setValueAndMoveForward()
            return false


            return true






            Also, I implemented this feature:



            enter image description here



            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



            class EnterConfirmationCodeTextField: UITextField 

            // MARK: Life cycle

            override func awakeFromNib()
            super.awakeFromNib()

            delegate = self


            // MARK: Methods

            override func deleteBackward()
            super.deleteBackward()

            let previousTag = self.tag - 1
            if let previousResponder = self.superview?.viewWithTag(previousTag)
            previousResponder.becomeFirstResponder()

            if let activeTextField = previousResponder as? UITextField
            if let isEmpty = activeTextField.text?.isEmpty, !isEmpty
            activeTextField.text = String()








            Variant B (a little bit another behavior):



            class EnterConfirmationCodeTextField: UITextField 

            // MARK: Life cycle

            override func awakeFromNib()
            super.awakeFromNib()

            delegate = self


            // MARK: Methods

            override func deleteBackward()
            super.deleteBackward()

            let previousTag = self.tag - 1
            if let previousResponder = self.superview?.viewWithTag(previousTag)
            previousResponder.becomeFirstResponder()






            Assign EnterConfirmationCodeTextField for each of your textFields and set they appropriate tag value.






            share|improve this answer





























              up vote
              1
              down vote













              enter image description here



              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



              func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
              guard let textField = textField as? PinCodeTextField else return true

              if string == "" // when the backward clicked, let it go :)
              return true


              // when textfield is not empty, well, next number
              if textField.pinCode.count == textField.maxCount
              becomeFirstResponder(after: textField)
              return false


              if string.count > textField.maxCharacterCount // the max character count should be 1
              return false

              return true

              // now the text field has been filled with a number
              func textFieldCotentDidChange(_ textField: UITextField)
              print("didchange")
              guard let textField = textField as? PinCodeTextField else return
              if textField.pinCode.count == 0
              becomeFirstResponder(before: textField)


              // when textfield has been filled, ok! next!
              if textField.pinCode.count == textField.maxCharacterCount
              becomeFirstResponder(after: textField)




              for more details and the simple demo, see this link






              share|improve this answer





























                up vote
                0
                down vote













                Just use TextFieldDelegate method and check the length of the TextField after every changes



                func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
                if newString.characters.count == 1

                nextTextField.becomeFirstResponder()
                return true

                else

                return false







                share|improve this answer



























                  up vote
                  0
                  down vote













                  Modified Anurag Soni's answer in Swift 3.



                  • It assumes you have outlet collection named textFields and the text fields have ordered tags set

                  • It adds case when there's already some digit in text field and when user types something new - the digit is replaced

                  • Input is restricted to digits only


                  • It prevents from pasting more than one digit



                    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                    // Restrict to only digits
                    let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
                    let compSepByCharInSet = string.components(separatedBy: aSet)
                    let numberFiltered = compSepByCharInSet.joined(separator: "")

                    if string != numberFiltered
                    return false


                    // Get the unwrapped text
                    guard let text = textField.text else
                    return false


                    if (text.characters.count < 1 && string.characters.count == 1)
                    // New value to empty text field
                    textField.text = string

                    // Next responder
                    if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                    someTextField.becomeFirstResponder()
                    else
                    view.endEditing(true)


                    return false
                    else if (text.characters.count >= 1 && string.characters.count == 0)
                    // On deleting value from Textfield
                    textField.text = ""

                    // Previous responder
                    if let someTextField = (textFields.filter $0.tag == textField.tag - 1 ).first
                    someTextField.becomeFirstResponder()
                    else
                    view.endEditing(true)


                    return false
                    else if string.characters.count == 1
                    // There's already some digit in text field
                    // Replace it with new one
                    textField.text = string

                    // Next responder
                    if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                    someTextField.becomeFirstResponder()
                    else
                    view.endEditing(true)



                    return false







                  share|improve this answer



























                    up vote
                    0
                    down vote













                    I was working on a similar functionality and did it in my way. Solution below.
                    Swift 4



                    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

                    //For clear button pressed
                    //If the textfield has already text in it
                    if string.count == 0
                    textField.text = string
                    return true


                    //For First time entry into the text field
                    guard let text = textField.text, text.count <= 0 else
                    //If user enter second character
                    return false

                    //For First time entry into the text field
                    if text.count == 0
                    textField.text = string
                    textField.resignFirstResponder()
                    self.nextResponde(tag: textField.tag)
                    return true

                    return false


                    //To make the next field as responder
                    func nextResponde(tag: Int)
                    switch tag
                    case self.PINTextField.tag:
                    guard let text = self.PINTextField1.text, text.count == 1 else
                    self.PINTextField1.becomeFirstResponder()
                    return

                    case self.PINTextField1.tag:
                    guard let text = self.PINTextField2.text, text.count == 1 else
                    self.PINTextField2.becomeFirstResponder()
                    return

                    case self.PINTextField2.tag:
                    guard let text = self.PINTextField3.text, text.count == 1 else
                    self.PINTextField3.becomeFirstResponder()
                    return

                    default:
                    let _ = tag







                    share|improve this answer



























                      up vote
                      -1
                      down vote













                      try this : - For swift 3.0



                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
                      let nextTag = textField.tag + 1;

                      // get next responder
                      let nextResponder = textField.superview?.viewWithTag(nextTag);
                      textField.text = string;

                      if (nextResponder == nil)
                      textField.resignFirstResponder()

                      nextResponder?.becomeFirstResponder();
                      return false;

                      else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
                      // on deleting value from Textfield
                      let previousTag = textField.tag - 1;

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1);

                      textField.text = "";
                      previousResponder?.becomeFirstResponder();
                      return false;

                      return true;



                      converted Anurag Soni answer in swift 3.0



                      You just have to implement this method only.






                      share|improve this answer




















                      • Anurag Soni's answer is completely wrong. When user tried to edit textfield again it will allow them to enter as many character as they can.
                        – Nij
                        Jul 12 '17 at 10:25










                      • @Nij I tested it and it was working fine here. And I only converted it to Swift 3.
                        – Sachin Nautiyal
                        Jul 17 '17 at 8:41










                      Your Answer






                      StackExchange.ifUsing("editor", function ()
                      StackExchange.using("externalEditor", function ()
                      StackExchange.using("snippets", function ()
                      StackExchange.snippets.init();
                      );
                      );
                      , "code-snippets");

                      StackExchange.ready(function()
                      var channelOptions =
                      tags: "".split(" "),
                      id: "1"
                      ;
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function()
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled)
                      StackExchange.using("snippets", function()
                      createEditor();
                      );

                      else
                      createEditor();

                      );

                      function createEditor()
                      StackExchange.prepareEditor(
                      heartbeatType: 'answer',
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader:
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      ,
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      );



                      );













                       

                      draft saved


                      draft discarded


















                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18711468%2fallowing-single-digit-in-uitextfield-in-ios%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown

























                      13 Answers
                      13






                      active

                      oldest

                      votes








                      13 Answers
                      13






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      up vote
                      17
                      down vote



                      accepted










                      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.



                      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                      if ((textField.text.length >= 1) && (string.length > 0))

                      NSInteger nextTag = textField.tag + 1;
                      // Try to find next responder
                      UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                      if (! nextResponder)
                      nextResponder = [textField.superview viewWithTag:1];

                      if (nextResponder)
                      // Found next responder, so set it.
                      [nextResponder becomeFirstResponder];

                      return NO;

                      return YES;



                      Swift 2



                      func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if (textField.text?.characters.count < 1 && string.characters.count > 0)
                      let nextTag = textField.tag + 1;

                      // get next responder
                      var nextResponder = textField.superview?.viewWithTag(nextTag);

                      if (nextResponder == nil)
                      nextResponder = textField.superview?.viewWithTag(1);

                      textField.text = string;
                      nextResponder?.becomeFirstResponder();
                      return false;

                      else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                      // on deleting value from Textfield
                      let previousTag = textField.tag - 1;

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1);

                      textField.text = "";
                      previousResponder?.becomeFirstResponder();
                      return false;

                      return true;



                      Swift 4



                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

                      if textField.text!.count < 1 && string.count > 0
                      let nextTag = textField.tag + 1

                      // get next responder
                      var nextResponder = textField.superview?.viewWithTag(nextTag)

                      if (nextResponder == nil)

                      nextResponder = textField.superview?.viewWithTag(1)

                      textField.text = string
                      nextResponder?.becomeFirstResponder()
                      return false

                      else if textField.text!.count >= 1 && string.count == 0
                      // on deleting value from Textfield
                      let previousTag = textField.tag - 1

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag)

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1)

                      textField.text = ""
                      previousResponder?.becomeFirstResponder()
                      return false

                      return true







                      share|improve this answer






















                      • HI Anurag thanks for the code, this is working like charm, but when i enter the next code in textbox1 ,cursor is going to textbox2 but code is not getting updated , how to solve ?
                        – krish
                        Sep 10 '13 at 6:10











                      • you want to enter one character in one textfield , am i getting right? so can you explain a little bit more what is your problem now
                        – Anurag Soni
                        Sep 10 '13 at 7:14











                      • @AnuragSoni i want to move automatic in nextfield
                        – Jigar
                        Jun 6 '16 at 5:12














                      up vote
                      17
                      down vote



                      accepted










                      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.



                      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                      if ((textField.text.length >= 1) && (string.length > 0))

                      NSInteger nextTag = textField.tag + 1;
                      // Try to find next responder
                      UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                      if (! nextResponder)
                      nextResponder = [textField.superview viewWithTag:1];

                      if (nextResponder)
                      // Found next responder, so set it.
                      [nextResponder becomeFirstResponder];

                      return NO;

                      return YES;



                      Swift 2



                      func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if (textField.text?.characters.count < 1 && string.characters.count > 0)
                      let nextTag = textField.tag + 1;

                      // get next responder
                      var nextResponder = textField.superview?.viewWithTag(nextTag);

                      if (nextResponder == nil)
                      nextResponder = textField.superview?.viewWithTag(1);

                      textField.text = string;
                      nextResponder?.becomeFirstResponder();
                      return false;

                      else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                      // on deleting value from Textfield
                      let previousTag = textField.tag - 1;

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1);

                      textField.text = "";
                      previousResponder?.becomeFirstResponder();
                      return false;

                      return true;



                      Swift 4



                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

                      if textField.text!.count < 1 && string.count > 0
                      let nextTag = textField.tag + 1

                      // get next responder
                      var nextResponder = textField.superview?.viewWithTag(nextTag)

                      if (nextResponder == nil)

                      nextResponder = textField.superview?.viewWithTag(1)

                      textField.text = string
                      nextResponder?.becomeFirstResponder()
                      return false

                      else if textField.text!.count >= 1 && string.count == 0
                      // on deleting value from Textfield
                      let previousTag = textField.tag - 1

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag)

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1)

                      textField.text = ""
                      previousResponder?.becomeFirstResponder()
                      return false

                      return true







                      share|improve this answer






















                      • HI Anurag thanks for the code, this is working like charm, but when i enter the next code in textbox1 ,cursor is going to textbox2 but code is not getting updated , how to solve ?
                        – krish
                        Sep 10 '13 at 6:10











                      • you want to enter one character in one textfield , am i getting right? so can you explain a little bit more what is your problem now
                        – Anurag Soni
                        Sep 10 '13 at 7:14











                      • @AnuragSoni i want to move automatic in nextfield
                        – Jigar
                        Jun 6 '16 at 5:12












                      up vote
                      17
                      down vote



                      accepted







                      up vote
                      17
                      down vote



                      accepted






                      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.



                      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                      if ((textField.text.length >= 1) && (string.length > 0))

                      NSInteger nextTag = textField.tag + 1;
                      // Try to find next responder
                      UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                      if (! nextResponder)
                      nextResponder = [textField.superview viewWithTag:1];

                      if (nextResponder)
                      // Found next responder, so set it.
                      [nextResponder becomeFirstResponder];

                      return NO;

                      return YES;



                      Swift 2



                      func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if (textField.text?.characters.count < 1 && string.characters.count > 0)
                      let nextTag = textField.tag + 1;

                      // get next responder
                      var nextResponder = textField.superview?.viewWithTag(nextTag);

                      if (nextResponder == nil)
                      nextResponder = textField.superview?.viewWithTag(1);

                      textField.text = string;
                      nextResponder?.becomeFirstResponder();
                      return false;

                      else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                      // on deleting value from Textfield
                      let previousTag = textField.tag - 1;

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1);

                      textField.text = "";
                      previousResponder?.becomeFirstResponder();
                      return false;

                      return true;



                      Swift 4



                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

                      if textField.text!.count < 1 && string.count > 0
                      let nextTag = textField.tag + 1

                      // get next responder
                      var nextResponder = textField.superview?.viewWithTag(nextTag)

                      if (nextResponder == nil)

                      nextResponder = textField.superview?.viewWithTag(1)

                      textField.text = string
                      nextResponder?.becomeFirstResponder()
                      return false

                      else if textField.text!.count >= 1 && string.count == 0
                      // on deleting value from Textfield
                      let previousTag = textField.tag - 1

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag)

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1)

                      textField.text = ""
                      previousResponder?.becomeFirstResponder()
                      return false

                      return true







                      share|improve this answer














                      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.



                      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                      if ((textField.text.length >= 1) && (string.length > 0))

                      NSInteger nextTag = textField.tag + 1;
                      // Try to find next responder
                      UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                      if (! nextResponder)
                      nextResponder = [textField.superview viewWithTag:1];

                      if (nextResponder)
                      // Found next responder, so set it.
                      [nextResponder becomeFirstResponder];

                      return NO;

                      return YES;



                      Swift 2



                      func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if (textField.text?.characters.count < 1 && string.characters.count > 0)
                      let nextTag = textField.tag + 1;

                      // get next responder
                      var nextResponder = textField.superview?.viewWithTag(nextTag);

                      if (nextResponder == nil)
                      nextResponder = textField.superview?.viewWithTag(1);

                      textField.text = string;
                      nextResponder?.becomeFirstResponder();
                      return false;

                      else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                      // on deleting value from Textfield
                      let previousTag = textField.tag - 1;

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1);

                      textField.text = "";
                      previousResponder?.becomeFirstResponder();
                      return false;

                      return true;



                      Swift 4



                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

                      if textField.text!.count < 1 && string.count > 0
                      let nextTag = textField.tag + 1

                      // get next responder
                      var nextResponder = textField.superview?.viewWithTag(nextTag)

                      if (nextResponder == nil)

                      nextResponder = textField.superview?.viewWithTag(1)

                      textField.text = string
                      nextResponder?.becomeFirstResponder()
                      return false

                      else if textField.text!.count >= 1 && string.count == 0
                      // on deleting value from Textfield
                      let previousTag = textField.tag - 1

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag)

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1)

                      textField.text = ""
                      previousResponder?.becomeFirstResponder()
                      return false

                      return true








                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 11 '17 at 15:10









                      Tamás Sengel

                      24.8k135992




                      24.8k135992










                      answered Sep 10 '13 at 6:01









                      Anurag Soni

                      859713




                      859713











                      • HI Anurag thanks for the code, this is working like charm, but when i enter the next code in textbox1 ,cursor is going to textbox2 but code is not getting updated , how to solve ?
                        – krish
                        Sep 10 '13 at 6:10











                      • you want to enter one character in one textfield , am i getting right? so can you explain a little bit more what is your problem now
                        – Anurag Soni
                        Sep 10 '13 at 7:14











                      • @AnuragSoni i want to move automatic in nextfield
                        – Jigar
                        Jun 6 '16 at 5:12
















                      • HI Anurag thanks for the code, this is working like charm, but when i enter the next code in textbox1 ,cursor is going to textbox2 but code is not getting updated , how to solve ?
                        – krish
                        Sep 10 '13 at 6:10











                      • you want to enter one character in one textfield , am i getting right? so can you explain a little bit more what is your problem now
                        – Anurag Soni
                        Sep 10 '13 at 7:14











                      • @AnuragSoni i want to move automatic in nextfield
                        – Jigar
                        Jun 6 '16 at 5:12















                      HI Anurag thanks for the code, this is working like charm, but when i enter the next code in textbox1 ,cursor is going to textbox2 but code is not getting updated , how to solve ?
                      – krish
                      Sep 10 '13 at 6:10





                      HI Anurag thanks for the code, this is working like charm, but when i enter the next code in textbox1 ,cursor is going to textbox2 but code is not getting updated , how to solve ?
                      – krish
                      Sep 10 '13 at 6:10













                      you want to enter one character in one textfield , am i getting right? so can you explain a little bit more what is your problem now
                      – Anurag Soni
                      Sep 10 '13 at 7:14





                      you want to enter one character in one textfield , am i getting right? so can you explain a little bit more what is your problem now
                      – Anurag Soni
                      Sep 10 '13 at 7:14













                      @AnuragSoni i want to move automatic in nextfield
                      – Jigar
                      Jun 6 '16 at 5:12




                      @AnuragSoni i want to move automatic in nextfield
                      – Jigar
                      Jun 6 '16 at 5:12












                      up vote
                      7
                      down vote













                      use this code if you don't want work with tag and it works better then above



                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
                      if(textField == txtOne)
                      txtTwo.becomeFirstResponder()

                      if(textField == txtTwo)
                      txtThree.becomeFirstResponder()

                      if(textField == txtThree)
                      txtFour.becomeFirstResponder()

                      textField.text = string
                      return false

                      else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
                      // on deleting value from Textfield
                      if(textField == txtTwo)
                      txtOne.becomeFirstResponder()

                      if(textField == txtThree)
                      txtTwo.becomeFirstResponder()

                      if(textField == txtFour)
                      txtThree.becomeFirstResponder()

                      textField.text = ""
                      return false
                      else if ((textField.text?.characters.count)! >= 1 )
                      textField.text = string
                      return false

                      return true






                      share|improve this answer


















                      • 1




                        This is acceptable answer, thanks.
                        – Nij
                        Jul 12 '17 at 10:32














                      up vote
                      7
                      down vote













                      use this code if you don't want work with tag and it works better then above



                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
                      if(textField == txtOne)
                      txtTwo.becomeFirstResponder()

                      if(textField == txtTwo)
                      txtThree.becomeFirstResponder()

                      if(textField == txtThree)
                      txtFour.becomeFirstResponder()

                      textField.text = string
                      return false

                      else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
                      // on deleting value from Textfield
                      if(textField == txtTwo)
                      txtOne.becomeFirstResponder()

                      if(textField == txtThree)
                      txtTwo.becomeFirstResponder()

                      if(textField == txtFour)
                      txtThree.becomeFirstResponder()

                      textField.text = ""
                      return false
                      else if ((textField.text?.characters.count)! >= 1 )
                      textField.text = string
                      return false

                      return true






                      share|improve this answer


















                      • 1




                        This is acceptable answer, thanks.
                        – Nij
                        Jul 12 '17 at 10:32












                      up vote
                      7
                      down vote










                      up vote
                      7
                      down vote









                      use this code if you don't want work with tag and it works better then above



                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
                      if(textField == txtOne)
                      txtTwo.becomeFirstResponder()

                      if(textField == txtTwo)
                      txtThree.becomeFirstResponder()

                      if(textField == txtThree)
                      txtFour.becomeFirstResponder()

                      textField.text = string
                      return false

                      else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
                      // on deleting value from Textfield
                      if(textField == txtTwo)
                      txtOne.becomeFirstResponder()

                      if(textField == txtThree)
                      txtTwo.becomeFirstResponder()

                      if(textField == txtFour)
                      txtThree.becomeFirstResponder()

                      textField.text = ""
                      return false
                      else if ((textField.text?.characters.count)! >= 1 )
                      textField.text = string
                      return false

                      return true






                      share|improve this answer














                      use this code if you don't want work with tag and it works better then above



                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
                      if(textField == txtOne)
                      txtTwo.becomeFirstResponder()

                      if(textField == txtTwo)
                      txtThree.becomeFirstResponder()

                      if(textField == txtThree)
                      txtFour.becomeFirstResponder()

                      textField.text = string
                      return false

                      else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
                      // on deleting value from Textfield
                      if(textField == txtTwo)
                      txtOne.becomeFirstResponder()

                      if(textField == txtThree)
                      txtTwo.becomeFirstResponder()

                      if(textField == txtFour)
                      txtThree.becomeFirstResponder()

                      textField.text = ""
                      return false
                      else if ((textField.text?.characters.count)! >= 1 )
                      textField.text = string
                      return false

                      return true







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 3 '17 at 5:24









                      Sourabh Sharma

                      5,28824867




                      5,28824867










                      answered Mar 31 '17 at 10:02









                      Varun Naharia

                      2,67432850




                      2,67432850







                      • 1




                        This is acceptable answer, thanks.
                        – Nij
                        Jul 12 '17 at 10:32












                      • 1




                        This is acceptable answer, thanks.
                        – Nij
                        Jul 12 '17 at 10:32







                      1




                      1




                      This is acceptable answer, thanks.
                      – Nij
                      Jul 12 '17 at 10:32




                      This is acceptable answer, thanks.
                      – Nij
                      Jul 12 '17 at 10:32










                      up vote
                      3
                      down vote













                      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.



                      func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                      // On inputing value to textfield
                      if (textField.text?.characters.count < 1 && string.characters.count > 0)
                      let nextTag = textField.tag + 1;

                      // get next responder
                      var nextResponder = textField.superview?.viewWithTag(nextTag);

                      if (nextResponder == nil)
                      nextResponder = textField.superview?.viewWithTag(1);

                      textField.text = string;
                      nextResponder?.becomeFirstResponder();
                      return false;

                      else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                      // on deleteing value from Textfield
                      let previousTag = textField.tag - 1;

                      // get next responder
                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                      if (previousResponder == nil)
                      previousResponder = textField.superview?.viewWithTag(1);

                      textField.text = "";
                      previousResponder?.becomeFirstResponder();
                      return false;

                      return true;






                      share|improve this answer
























                        up vote
                        3
                        down vote













                        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.



                        func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                        // On inputing value to textfield
                        if (textField.text?.characters.count < 1 && string.characters.count > 0)
                        let nextTag = textField.tag + 1;

                        // get next responder
                        var nextResponder = textField.superview?.viewWithTag(nextTag);

                        if (nextResponder == nil)
                        nextResponder = textField.superview?.viewWithTag(1);

                        textField.text = string;
                        nextResponder?.becomeFirstResponder();
                        return false;

                        else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                        // on deleteing value from Textfield
                        let previousTag = textField.tag - 1;

                        // get next responder
                        var previousResponder = textField.superview?.viewWithTag(previousTag);

                        if (previousResponder == nil)
                        previousResponder = textField.superview?.viewWithTag(1);

                        textField.text = "";
                        previousResponder?.becomeFirstResponder();
                        return false;

                        return true;






                        share|improve this answer






















                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote









                          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.



                          func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                          // On inputing value to textfield
                          if (textField.text?.characters.count < 1 && string.characters.count > 0)
                          let nextTag = textField.tag + 1;

                          // get next responder
                          var nextResponder = textField.superview?.viewWithTag(nextTag);

                          if (nextResponder == nil)
                          nextResponder = textField.superview?.viewWithTag(1);

                          textField.text = string;
                          nextResponder?.becomeFirstResponder();
                          return false;

                          else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                          // on deleteing value from Textfield
                          let previousTag = textField.tag - 1;

                          // get next responder
                          var previousResponder = textField.superview?.viewWithTag(previousTag);

                          if (previousResponder == nil)
                          previousResponder = textField.superview?.viewWithTag(1);

                          textField.text = "";
                          previousResponder?.becomeFirstResponder();
                          return false;

                          return true;






                          share|improve this answer












                          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.



                          func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                          // On inputing value to textfield
                          if (textField.text?.characters.count < 1 && string.characters.count > 0)
                          let nextTag = textField.tag + 1;

                          // get next responder
                          var nextResponder = textField.superview?.viewWithTag(nextTag);

                          if (nextResponder == nil)
                          nextResponder = textField.superview?.viewWithTag(1);

                          textField.text = string;
                          nextResponder?.becomeFirstResponder();
                          return false;

                          else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                          // on deleteing value from Textfield
                          let previousTag = textField.tag - 1;

                          // get next responder
                          var previousResponder = textField.superview?.viewWithTag(previousTag);

                          if (previousResponder == nil)
                          previousResponder = textField.superview?.viewWithTag(1);

                          textField.text = "";
                          previousResponder?.becomeFirstResponder();
                          return false;

                          return true;







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Oct 7 '15 at 20:22









                          silentBeep

                          5,552134448




                          5,552134448




















                              up vote
                              2
                              down vote













                              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



                              - (void)startPinCode

                              txtPinCodeLockDigits.text = @"";

                              for (int i = 1; i <= 4; i++)

                              UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
                              [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


                              [txtPinCodeLockDigits becomeFirstResponder];



                              Then change imageview's images as per user input and only allow four characters



                              - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                              NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
                              textField.text = result;

                              for (int i = 1; i <= 4; i++)

                              UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
                              if (i <= [result length])
                              [img setImage:[UIImage imageNamed:@"Img_BG_PinCode_Filled.png"]];
                              else
                              [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


                              NSLog(@"Result :: %@", result);

                              if ([result length] == 4)
                              [self performSelector:@selector(keyGenerationForApplication:) withObject:result afterDelay:0.2];


                              return NO;



                              After Four characters call function for generated PIN Code and store it in User Defaults same as iOS default PIN settings



                              - (void)keyGenerationForApplication:(NSString *)pinCode

                              int appCode = [pinCode intValue];
                              [DefaultsValues setIntegerValueToUserDefaults:appCode ForKey:PIN_LOCK_PATTERN];



                              Here, you can again call StartPinCode method for re-confirming code.



                              Hopefully, it'll help you.

                              Thanks






                              share|improve this answer
























                                up vote
                                2
                                down vote













                                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



                                - (void)startPinCode

                                txtPinCodeLockDigits.text = @"";

                                for (int i = 1; i <= 4; i++)

                                UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
                                [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


                                [txtPinCodeLockDigits becomeFirstResponder];



                                Then change imageview's images as per user input and only allow four characters



                                - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
                                textField.text = result;

                                for (int i = 1; i <= 4; i++)

                                UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
                                if (i <= [result length])
                                [img setImage:[UIImage imageNamed:@"Img_BG_PinCode_Filled.png"]];
                                else
                                [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


                                NSLog(@"Result :: %@", result);

                                if ([result length] == 4)
                                [self performSelector:@selector(keyGenerationForApplication:) withObject:result afterDelay:0.2];


                                return NO;



                                After Four characters call function for generated PIN Code and store it in User Defaults same as iOS default PIN settings



                                - (void)keyGenerationForApplication:(NSString *)pinCode

                                int appCode = [pinCode intValue];
                                [DefaultsValues setIntegerValueToUserDefaults:appCode ForKey:PIN_LOCK_PATTERN];



                                Here, you can again call StartPinCode method for re-confirming code.



                                Hopefully, it'll help you.

                                Thanks






                                share|improve this answer






















                                  up vote
                                  2
                                  down vote










                                  up vote
                                  2
                                  down vote









                                  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



                                  - (void)startPinCode

                                  txtPinCodeLockDigits.text = @"";

                                  for (int i = 1; i <= 4; i++)

                                  UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
                                  [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


                                  [txtPinCodeLockDigits becomeFirstResponder];



                                  Then change imageview's images as per user input and only allow four characters



                                  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                  NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
                                  textField.text = result;

                                  for (int i = 1; i <= 4; i++)

                                  UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
                                  if (i <= [result length])
                                  [img setImage:[UIImage imageNamed:@"Img_BG_PinCode_Filled.png"]];
                                  else
                                  [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


                                  NSLog(@"Result :: %@", result);

                                  if ([result length] == 4)
                                  [self performSelector:@selector(keyGenerationForApplication:) withObject:result afterDelay:0.2];


                                  return NO;



                                  After Four characters call function for generated PIN Code and store it in User Defaults same as iOS default PIN settings



                                  - (void)keyGenerationForApplication:(NSString *)pinCode

                                  int appCode = [pinCode intValue];
                                  [DefaultsValues setIntegerValueToUserDefaults:appCode ForKey:PIN_LOCK_PATTERN];



                                  Here, you can again call StartPinCode method for re-confirming code.



                                  Hopefully, it'll help you.

                                  Thanks






                                  share|improve this answer












                                  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



                                  - (void)startPinCode

                                  txtPinCodeLockDigits.text = @"";

                                  for (int i = 1; i <= 4; i++)

                                  UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
                                  [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


                                  [txtPinCodeLockDigits becomeFirstResponder];



                                  Then change imageview's images as per user input and only allow four characters



                                  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                  NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
                                  textField.text = result;

                                  for (int i = 1; i <= 4; i++)

                                  UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
                                  if (i <= [result length])
                                  [img setImage:[UIImage imageNamed:@"Img_BG_PinCode_Filled.png"]];
                                  else
                                  [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];


                                  NSLog(@"Result :: %@", result);

                                  if ([result length] == 4)
                                  [self performSelector:@selector(keyGenerationForApplication:) withObject:result afterDelay:0.2];


                                  return NO;



                                  After Four characters call function for generated PIN Code and store it in User Defaults same as iOS default PIN settings



                                  - (void)keyGenerationForApplication:(NSString *)pinCode

                                  int appCode = [pinCode intValue];
                                  [DefaultsValues setIntegerValueToUserDefaults:appCode ForKey:PIN_LOCK_PATTERN];



                                  Here, you can again call StartPinCode method for re-confirming code.



                                  Hopefully, it'll help you.

                                  Thanks







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Sep 10 '13 at 5:58









                                  MS.

                                  2,38522147




                                  2,38522147




















                                      up vote
                                      2
                                      down vote













                                      swift 2.3



                                       class BankDepositsWithOTPVC: UIViewController 

                                      let limitLength = 1

                                      override func viewDidLoad()
                                      super.viewDidLoad()





                                      // MARK: Textfield Validator

                                      extension BankDepositsWithOTPVC : UITextFieldDelegate


                                      func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
                                      // On inputing value to textfield
                                      if (textField.text?.characters.count < 1 && string.characters.count > 0)
                                      let nextTag = textField.tag + 1;

                                      // get next responder
                                      let nextResponder = textField.superview?.viewWithTag(nextTag);

                                      if (nextResponder == nil)
                                      textField.resignFirstResponder()
                                      // nextResponder = textField.superview?.viewWithTag(1);

                                      textField.text = string;
                                      nextResponder?.becomeFirstResponder();
                                      return false;

                                      else if (textField.text?.characters.count >= 1 && string.characters.count > 0)
                                      // maximum 1 digit

                                      textField.text = "";

                                      let nextTag = textField.tag + 1;

                                      // get next responder
                                      let nextResponder = textField.superview?.viewWithTag(nextTag);

                                      if (nextResponder == nil)
                                      textField.resignFirstResponder()
                                      // nextResponder = textField.superview?.viewWithTag(1);

                                      textField.text = string;
                                      nextResponder?.becomeFirstResponder();
                                      return false;

                                      else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                                      // on deleteing value from Textfield
                                      let previousTag = textField.tag - 1;

                                      // get next responder
                                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                                      if (previousResponder == nil)
                                      previousResponder = textField.superview?.viewWithTag(1);

                                      textField.text = "";
                                      previousResponder?.becomeFirstResponder();
                                      return false;

                                      //return true;

                                      guard let text = textField.text else return true
                                      let newLength = text.characters.count + string.characters.count - range.length
                                      return newLength <= limitLength






                                      Objective-C



                                      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                      if ((textField.text.length < 1) && (string.length > 0))


                                      NSInteger nextTag = textField.tag + 1;
                                      UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                                      if (! nextResponder)
                                      [textField resignFirstResponder];

                                      textField.text = string;
                                      if (nextResponder)
                                      [nextResponder becomeFirstResponder];

                                      return NO;

                                      else if ((textField.text.length >= 1) && (string.length > 0))
                                      //FOR MAXIMUM 1 TEXT

                                      NSInteger nextTag = textField.tag + 1;
                                      UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                                      if (! nextResponder)
                                      [textField resignFirstResponder];

                                      textField.text = string;
                                      if (nextResponder)
                                      [nextResponder becomeFirstResponder];

                                      return NO;

                                      else if ((textField.text.length >= 1) && (string.length == 0))
                                      // on deleteing value from Textfield

                                      NSInteger prevTag = textField.tag - 1;
                                      // Try to find prev responder
                                      UIResponder* prevResponder = [textField.superview viewWithTag:prevTag];
                                      if (! prevResponder)
                                      [textField resignFirstResponder];

                                      textField.text = string;
                                      if (prevResponder)
                                      // Found next responder, so set it.
                                      [prevResponder becomeFirstResponder];

                                      return NO;


                                      return YES;






                                      share|improve this answer


























                                        up vote
                                        2
                                        down vote













                                        swift 2.3



                                         class BankDepositsWithOTPVC: UIViewController 

                                        let limitLength = 1

                                        override func viewDidLoad()
                                        super.viewDidLoad()





                                        // MARK: Textfield Validator

                                        extension BankDepositsWithOTPVC : UITextFieldDelegate


                                        func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
                                        // On inputing value to textfield
                                        if (textField.text?.characters.count < 1 && string.characters.count > 0)
                                        let nextTag = textField.tag + 1;

                                        // get next responder
                                        let nextResponder = textField.superview?.viewWithTag(nextTag);

                                        if (nextResponder == nil)
                                        textField.resignFirstResponder()
                                        // nextResponder = textField.superview?.viewWithTag(1);

                                        textField.text = string;
                                        nextResponder?.becomeFirstResponder();
                                        return false;

                                        else if (textField.text?.characters.count >= 1 && string.characters.count > 0)
                                        // maximum 1 digit

                                        textField.text = "";

                                        let nextTag = textField.tag + 1;

                                        // get next responder
                                        let nextResponder = textField.superview?.viewWithTag(nextTag);

                                        if (nextResponder == nil)
                                        textField.resignFirstResponder()
                                        // nextResponder = textField.superview?.viewWithTag(1);

                                        textField.text = string;
                                        nextResponder?.becomeFirstResponder();
                                        return false;

                                        else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                                        // on deleteing value from Textfield
                                        let previousTag = textField.tag - 1;

                                        // get next responder
                                        var previousResponder = textField.superview?.viewWithTag(previousTag);

                                        if (previousResponder == nil)
                                        previousResponder = textField.superview?.viewWithTag(1);

                                        textField.text = "";
                                        previousResponder?.becomeFirstResponder();
                                        return false;

                                        //return true;

                                        guard let text = textField.text else return true
                                        let newLength = text.characters.count + string.characters.count - range.length
                                        return newLength <= limitLength






                                        Objective-C



                                        - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                        if ((textField.text.length < 1) && (string.length > 0))


                                        NSInteger nextTag = textField.tag + 1;
                                        UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                                        if (! nextResponder)
                                        [textField resignFirstResponder];

                                        textField.text = string;
                                        if (nextResponder)
                                        [nextResponder becomeFirstResponder];

                                        return NO;

                                        else if ((textField.text.length >= 1) && (string.length > 0))
                                        //FOR MAXIMUM 1 TEXT

                                        NSInteger nextTag = textField.tag + 1;
                                        UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                                        if (! nextResponder)
                                        [textField resignFirstResponder];

                                        textField.text = string;
                                        if (nextResponder)
                                        [nextResponder becomeFirstResponder];

                                        return NO;

                                        else if ((textField.text.length >= 1) && (string.length == 0))
                                        // on deleteing value from Textfield

                                        NSInteger prevTag = textField.tag - 1;
                                        // Try to find prev responder
                                        UIResponder* prevResponder = [textField.superview viewWithTag:prevTag];
                                        if (! prevResponder)
                                        [textField resignFirstResponder];

                                        textField.text = string;
                                        if (prevResponder)
                                        // Found next responder, so set it.
                                        [prevResponder becomeFirstResponder];

                                        return NO;


                                        return YES;






                                        share|improve this answer
























                                          up vote
                                          2
                                          down vote










                                          up vote
                                          2
                                          down vote









                                          swift 2.3



                                           class BankDepositsWithOTPVC: UIViewController 

                                          let limitLength = 1

                                          override func viewDidLoad()
                                          super.viewDidLoad()





                                          // MARK: Textfield Validator

                                          extension BankDepositsWithOTPVC : UITextFieldDelegate


                                          func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
                                          // On inputing value to textfield
                                          if (textField.text?.characters.count < 1 && string.characters.count > 0)
                                          let nextTag = textField.tag + 1;

                                          // get next responder
                                          let nextResponder = textField.superview?.viewWithTag(nextTag);

                                          if (nextResponder == nil)
                                          textField.resignFirstResponder()
                                          // nextResponder = textField.superview?.viewWithTag(1);

                                          textField.text = string;
                                          nextResponder?.becomeFirstResponder();
                                          return false;

                                          else if (textField.text?.characters.count >= 1 && string.characters.count > 0)
                                          // maximum 1 digit

                                          textField.text = "";

                                          let nextTag = textField.tag + 1;

                                          // get next responder
                                          let nextResponder = textField.superview?.viewWithTag(nextTag);

                                          if (nextResponder == nil)
                                          textField.resignFirstResponder()
                                          // nextResponder = textField.superview?.viewWithTag(1);

                                          textField.text = string;
                                          nextResponder?.becomeFirstResponder();
                                          return false;

                                          else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                                          // on deleteing value from Textfield
                                          let previousTag = textField.tag - 1;

                                          // get next responder
                                          var previousResponder = textField.superview?.viewWithTag(previousTag);

                                          if (previousResponder == nil)
                                          previousResponder = textField.superview?.viewWithTag(1);

                                          textField.text = "";
                                          previousResponder?.becomeFirstResponder();
                                          return false;

                                          //return true;

                                          guard let text = textField.text else return true
                                          let newLength = text.characters.count + string.characters.count - range.length
                                          return newLength <= limitLength






                                          Objective-C



                                          - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                          if ((textField.text.length < 1) && (string.length > 0))


                                          NSInteger nextTag = textField.tag + 1;
                                          UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                                          if (! nextResponder)
                                          [textField resignFirstResponder];

                                          textField.text = string;
                                          if (nextResponder)
                                          [nextResponder becomeFirstResponder];

                                          return NO;

                                          else if ((textField.text.length >= 1) && (string.length > 0))
                                          //FOR MAXIMUM 1 TEXT

                                          NSInteger nextTag = textField.tag + 1;
                                          UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                                          if (! nextResponder)
                                          [textField resignFirstResponder];

                                          textField.text = string;
                                          if (nextResponder)
                                          [nextResponder becomeFirstResponder];

                                          return NO;

                                          else if ((textField.text.length >= 1) && (string.length == 0))
                                          // on deleteing value from Textfield

                                          NSInteger prevTag = textField.tag - 1;
                                          // Try to find prev responder
                                          UIResponder* prevResponder = [textField.superview viewWithTag:prevTag];
                                          if (! prevResponder)
                                          [textField resignFirstResponder];

                                          textField.text = string;
                                          if (prevResponder)
                                          // Found next responder, so set it.
                                          [prevResponder becomeFirstResponder];

                                          return NO;


                                          return YES;






                                          share|improve this answer














                                          swift 2.3



                                           class BankDepositsWithOTPVC: UIViewController 

                                          let limitLength = 1

                                          override func viewDidLoad()
                                          super.viewDidLoad()





                                          // MARK: Textfield Validator

                                          extension BankDepositsWithOTPVC : UITextFieldDelegate


                                          func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
                                          // On inputing value to textfield
                                          if (textField.text?.characters.count < 1 && string.characters.count > 0)
                                          let nextTag = textField.tag + 1;

                                          // get next responder
                                          let nextResponder = textField.superview?.viewWithTag(nextTag);

                                          if (nextResponder == nil)
                                          textField.resignFirstResponder()
                                          // nextResponder = textField.superview?.viewWithTag(1);

                                          textField.text = string;
                                          nextResponder?.becomeFirstResponder();
                                          return false;

                                          else if (textField.text?.characters.count >= 1 && string.characters.count > 0)
                                          // maximum 1 digit

                                          textField.text = "";

                                          let nextTag = textField.tag + 1;

                                          // get next responder
                                          let nextResponder = textField.superview?.viewWithTag(nextTag);

                                          if (nextResponder == nil)
                                          textField.resignFirstResponder()
                                          // nextResponder = textField.superview?.viewWithTag(1);

                                          textField.text = string;
                                          nextResponder?.becomeFirstResponder();
                                          return false;

                                          else if (textField.text?.characters.count >= 1 && string.characters.count == 0)
                                          // on deleteing value from Textfield
                                          let previousTag = textField.tag - 1;

                                          // get next responder
                                          var previousResponder = textField.superview?.viewWithTag(previousTag);

                                          if (previousResponder == nil)
                                          previousResponder = textField.superview?.viewWithTag(1);

                                          textField.text = "";
                                          previousResponder?.becomeFirstResponder();
                                          return false;

                                          //return true;

                                          guard let text = textField.text else return true
                                          let newLength = text.characters.count + string.characters.count - range.length
                                          return newLength <= limitLength






                                          Objective-C



                                          - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                          if ((textField.text.length < 1) && (string.length > 0))


                                          NSInteger nextTag = textField.tag + 1;
                                          UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                                          if (! nextResponder)
                                          [textField resignFirstResponder];

                                          textField.text = string;
                                          if (nextResponder)
                                          [nextResponder becomeFirstResponder];

                                          return NO;

                                          else if ((textField.text.length >= 1) && (string.length > 0))
                                          //FOR MAXIMUM 1 TEXT

                                          NSInteger nextTag = textField.tag + 1;
                                          UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
                                          if (! nextResponder)
                                          [textField resignFirstResponder];

                                          textField.text = string;
                                          if (nextResponder)
                                          [nextResponder becomeFirstResponder];

                                          return NO;

                                          else if ((textField.text.length >= 1) && (string.length == 0))
                                          // on deleteing value from Textfield

                                          NSInteger prevTag = textField.tag - 1;
                                          // Try to find prev responder
                                          UIResponder* prevResponder = [textField.superview viewWithTag:prevTag];
                                          if (! prevResponder)
                                          [textField resignFirstResponder];

                                          textField.text = string;
                                          if (prevResponder)
                                          // Found next responder, so set it.
                                          [prevResponder becomeFirstResponder];

                                          return NO;


                                          return YES;







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Feb 26 '17 at 8:27

























                                          answered Feb 26 '17 at 8:07









                                          Nischal Hada

                                          2,17212047




                                          2,17212047




















                                              up vote
                                              2
                                              down vote













                                              Provide the tag to the textfield like 1,2,3,4 and directly use it



                                              func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool


                                              let next:NSInteger

                                              if string == ""

                                              next = textField.tag - 1;


                                              else

                                              next = textField.tag + 1;



                                              if (textField.text?.characters.count)! >= 1

                                              if textField.tag == 4

                                              if string == ""

                                              textField.text = ""

                                              let temptf = self.view.viewWithTag(next) as! UITextField

                                              temptf.becomeFirstResponder()

                                              return false


                                              else

                                              if (textField.text?.characters.count)! > 1

                                              let stringg = textField.text!
                                              textField.text = stringg.replacingOccurrences(of: stringg, with: string)

                                              return false


                                              else
                                              if string == ""

                                              textField.text = ""

                                              if next != 0

                                              let temptf = self.view.viewWithTag(next) as! UITextField
                                              temptf.becomeFirstResponder()



                                              return false


                                              else

                                              if (textField.text?.characters.count)! > 1


                                              let stringg = textField.text!
                                              textField.text = stringg.replacingOccurrences(of: stringg, with: string)



                                              let temptf = self.view.viewWithTag(next) as! UITextField

                                              temptf.becomeFirstResponder()





                                              return true

                                              }





                                              share|improve this answer
























                                                up vote
                                                2
                                                down vote













                                                Provide the tag to the textfield like 1,2,3,4 and directly use it



                                                func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool


                                                let next:NSInteger

                                                if string == ""

                                                next = textField.tag - 1;


                                                else

                                                next = textField.tag + 1;



                                                if (textField.text?.characters.count)! >= 1

                                                if textField.tag == 4

                                                if string == ""

                                                textField.text = ""

                                                let temptf = self.view.viewWithTag(next) as! UITextField

                                                temptf.becomeFirstResponder()

                                                return false


                                                else

                                                if (textField.text?.characters.count)! > 1

                                                let stringg = textField.text!
                                                textField.text = stringg.replacingOccurrences(of: stringg, with: string)

                                                return false


                                                else
                                                if string == ""

                                                textField.text = ""

                                                if next != 0

                                                let temptf = self.view.viewWithTag(next) as! UITextField
                                                temptf.becomeFirstResponder()



                                                return false


                                                else

                                                if (textField.text?.characters.count)! > 1


                                                let stringg = textField.text!
                                                textField.text = stringg.replacingOccurrences(of: stringg, with: string)



                                                let temptf = self.view.viewWithTag(next) as! UITextField

                                                temptf.becomeFirstResponder()





                                                return true

                                                }





                                                share|improve this answer






















                                                  up vote
                                                  2
                                                  down vote










                                                  up vote
                                                  2
                                                  down vote









                                                  Provide the tag to the textfield like 1,2,3,4 and directly use it



                                                  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool


                                                  let next:NSInteger

                                                  if string == ""

                                                  next = textField.tag - 1;


                                                  else

                                                  next = textField.tag + 1;



                                                  if (textField.text?.characters.count)! >= 1

                                                  if textField.tag == 4

                                                  if string == ""

                                                  textField.text = ""

                                                  let temptf = self.view.viewWithTag(next) as! UITextField

                                                  temptf.becomeFirstResponder()

                                                  return false


                                                  else

                                                  if (textField.text?.characters.count)! > 1

                                                  let stringg = textField.text!
                                                  textField.text = stringg.replacingOccurrences(of: stringg, with: string)

                                                  return false


                                                  else
                                                  if string == ""

                                                  textField.text = ""

                                                  if next != 0

                                                  let temptf = self.view.viewWithTag(next) as! UITextField
                                                  temptf.becomeFirstResponder()



                                                  return false


                                                  else

                                                  if (textField.text?.characters.count)! > 1


                                                  let stringg = textField.text!
                                                  textField.text = stringg.replacingOccurrences(of: stringg, with: string)



                                                  let temptf = self.view.viewWithTag(next) as! UITextField

                                                  temptf.becomeFirstResponder()





                                                  return true

                                                  }





                                                  share|improve this answer












                                                  Provide the tag to the textfield like 1,2,3,4 and directly use it



                                                  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool


                                                  let next:NSInteger

                                                  if string == ""

                                                  next = textField.tag - 1;


                                                  else

                                                  next = textField.tag + 1;



                                                  if (textField.text?.characters.count)! >= 1

                                                  if textField.tag == 4

                                                  if string == ""

                                                  textField.text = ""

                                                  let temptf = self.view.viewWithTag(next) as! UITextField

                                                  temptf.becomeFirstResponder()

                                                  return false


                                                  else

                                                  if (textField.text?.characters.count)! > 1

                                                  let stringg = textField.text!
                                                  textField.text = stringg.replacingOccurrences(of: stringg, with: string)

                                                  return false


                                                  else
                                                  if string == ""

                                                  textField.text = ""

                                                  if next != 0

                                                  let temptf = self.view.viewWithTag(next) as! UITextField
                                                  temptf.becomeFirstResponder()



                                                  return false


                                                  else

                                                  if (textField.text?.characters.count)! > 1


                                                  let stringg = textField.text!
                                                  textField.text = stringg.replacingOccurrences(of: stringg, with: string)



                                                  let temptf = self.view.viewWithTag(next) as! UITextField

                                                  temptf.becomeFirstResponder()





                                                  return true

                                                  }






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Mar 3 '17 at 7:04









                                                  iSlimshady

                                                  211




                                                  211




















                                                      up vote
                                                      1
                                                      down vote













                                                      Try this
                                                      sample tutorial passcode lock



                                                      ViewController.h



                                                      #import <UIKit/UIKit.h>
                                                      @interface ViewController : UIViewController<UITextFieldDelegate>

                                                      IBOutlet UITextField *txtPassword;

                                                      @end


                                                      ViewController.m



                                                      - (void)viewDidLoad

                                                      [super viewDidLoad];
                                                      txtPassword.delegate=self;



                                                      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                                      NSUInteger newLength = [textField.text length] + [string length] - range.length;
                                                      return (newLength > 1) ? NO : YES;






                                                      share|improve this answer


























                                                        up vote
                                                        1
                                                        down vote













                                                        Try this
                                                        sample tutorial passcode lock



                                                        ViewController.h



                                                        #import <UIKit/UIKit.h>
                                                        @interface ViewController : UIViewController<UITextFieldDelegate>

                                                        IBOutlet UITextField *txtPassword;

                                                        @end


                                                        ViewController.m



                                                        - (void)viewDidLoad

                                                        [super viewDidLoad];
                                                        txtPassword.delegate=self;



                                                        - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                                        NSUInteger newLength = [textField.text length] + [string length] - range.length;
                                                        return (newLength > 1) ? NO : YES;






                                                        share|improve this answer
























                                                          up vote
                                                          1
                                                          down vote










                                                          up vote
                                                          1
                                                          down vote









                                                          Try this
                                                          sample tutorial passcode lock



                                                          ViewController.h



                                                          #import <UIKit/UIKit.h>
                                                          @interface ViewController : UIViewController<UITextFieldDelegate>

                                                          IBOutlet UITextField *txtPassword;

                                                          @end


                                                          ViewController.m



                                                          - (void)viewDidLoad

                                                          [super viewDidLoad];
                                                          txtPassword.delegate=self;



                                                          - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                                          NSUInteger newLength = [textField.text length] + [string length] - range.length;
                                                          return (newLength > 1) ? NO : YES;






                                                          share|improve this answer














                                                          Try this
                                                          sample tutorial passcode lock



                                                          ViewController.h



                                                          #import <UIKit/UIKit.h>
                                                          @interface ViewController : UIViewController<UITextFieldDelegate>

                                                          IBOutlet UITextField *txtPassword;

                                                          @end


                                                          ViewController.m



                                                          - (void)viewDidLoad

                                                          [super viewDidLoad];
                                                          txtPassword.delegate=self;



                                                          - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

                                                          NSUInteger newLength = [textField.text length] + [string length] - range.length;
                                                          return (newLength > 1) ? NO : YES;







                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Sep 10 '13 at 6:35

























                                                          answered Sep 10 '13 at 6:22









                                                          Ravindhiran

                                                          2,85833872




                                                          2,85833872




















                                                              up vote
                                                              1
                                                              down vote













                                                              Swift 4



                                                              Inspired by @Anurag Soni and @Varun Naharia answers



                                                              Variant A



                                                              extension EnterConfirmationCodeTextField: UITextFieldDelegate 

                                                              func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
                                                              guard let textFieldCount = textField.text?.count else return false

                                                              // Сlosure
                                                              let setValueAndMoveForward =
                                                              textField.text = string
                                                              let nextTag = textField.tag + 1
                                                              if let nextResponder = textField.superview?.viewWithTag(nextTag)
                                                              nextResponder.becomeFirstResponder()



                                                              // Сlosure
                                                              let clearValueAndMoveBack =
                                                              textField.text = ""
                                                              let previousTag = textField.tag - 1
                                                              if let previousResponder = textField.superview?.viewWithTag(previousTag)
                                                              previousResponder.becomeFirstResponder()



                                                              if textFieldCount < 1 && string.count > 0

                                                              setValueAndMoveForward()

                                                              if textField.tag == 4
                                                              print("Do something")


                                                              return false

                                                              else if textFieldCount >= 1 && string.count == 0

                                                              clearValueAndMoveBack()
                                                              return false

                                                              else if textFieldCount >= 1 && string.count > 0

                                                              let nextTag = self.tag + 1
                                                              if let previousResponder = self.superview?.viewWithTag(nextTag)
                                                              previousResponder.becomeFirstResponder()

                                                              if let activeTextField = previousResponder as? UITextField
                                                              activeTextField.text = string



                                                              return false


                                                              return true






                                                              Variant B (a little bit another behavior):



                                                              extension EnterConfirmationCodeTextField: UITextFieldDelegate 

                                                              func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
                                                              guard let textFieldCount = textField.text?.count else return false

                                                              // Сlosure
                                                              let setValueAndMoveForward =
                                                              textField.text = string
                                                              let nextTag = textField.tag + 1
                                                              if let nextResponder = textField.superview?.viewWithTag(nextTag)
                                                              nextResponder.becomeFirstResponder()



                                                              // Сlosure
                                                              let clearValueAndMoveBack =
                                                              textField.text = ""
                                                              let previousTag = textField.tag - 1
                                                              if let previousResponder = textField.superview?.viewWithTag(previousTag)
                                                              previousResponder.becomeFirstResponder()



                                                              if textFieldCount < 1 && string.count > 0

                                                              setValueAndMoveForward()

                                                              if textField.tag == 4
                                                              print("Do something")


                                                              return false

                                                              else if textFieldCount >= 1 && string.count == 0

                                                              clearValueAndMoveBack()
                                                              return false

                                                              else if textFieldCount >= 1

                                                              setValueAndMoveForward()
                                                              return false


                                                              return true






                                                              Also, I implemented this feature:



                                                              enter image description here



                                                              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



                                                              class EnterConfirmationCodeTextField: UITextField 

                                                              // MARK: Life cycle

                                                              override func awakeFromNib()
                                                              super.awakeFromNib()

                                                              delegate = self


                                                              // MARK: Methods

                                                              override func deleteBackward()
                                                              super.deleteBackward()

                                                              let previousTag = self.tag - 1
                                                              if let previousResponder = self.superview?.viewWithTag(previousTag)
                                                              previousResponder.becomeFirstResponder()

                                                              if let activeTextField = previousResponder as? UITextField
                                                              if let isEmpty = activeTextField.text?.isEmpty, !isEmpty
                                                              activeTextField.text = String()








                                                              Variant B (a little bit another behavior):



                                                              class EnterConfirmationCodeTextField: UITextField 

                                                              // MARK: Life cycle

                                                              override func awakeFromNib()
                                                              super.awakeFromNib()

                                                              delegate = self


                                                              // MARK: Methods

                                                              override func deleteBackward()
                                                              super.deleteBackward()

                                                              let previousTag = self.tag - 1
                                                              if let previousResponder = self.superview?.viewWithTag(previousTag)
                                                              previousResponder.becomeFirstResponder()






                                                              Assign EnterConfirmationCodeTextField for each of your textFields and set they appropriate tag value.






                                                              share|improve this answer


























                                                                up vote
                                                                1
                                                                down vote













                                                                Swift 4



                                                                Inspired by @Anurag Soni and @Varun Naharia answers



                                                                Variant A



                                                                extension EnterConfirmationCodeTextField: UITextFieldDelegate 

                                                                func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
                                                                guard let textFieldCount = textField.text?.count else return false

                                                                // Сlosure
                                                                let setValueAndMoveForward =
                                                                textField.text = string
                                                                let nextTag = textField.tag + 1
                                                                if let nextResponder = textField.superview?.viewWithTag(nextTag)
                                                                nextResponder.becomeFirstResponder()



                                                                // Сlosure
                                                                let clearValueAndMoveBack =
                                                                textField.text = ""
                                                                let previousTag = textField.tag - 1
                                                                if let previousResponder = textField.superview?.viewWithTag(previousTag)
                                                                previousResponder.becomeFirstResponder()



                                                                if textFieldCount < 1 && string.count > 0

                                                                setValueAndMoveForward()

                                                                if textField.tag == 4
                                                                print("Do something")


                                                                return false

                                                                else if textFieldCount >= 1 && string.count == 0

                                                                clearValueAndMoveBack()
                                                                return false

                                                                else if textFieldCount >= 1 && string.count > 0

                                                                let nextTag = self.tag + 1
                                                                if let previousResponder = self.superview?.viewWithTag(nextTag)
                                                                previousResponder.becomeFirstResponder()

                                                                if let activeTextField = previousResponder as? UITextField
                                                                activeTextField.text = string



                                                                return false


                                                                return true






                                                                Variant B (a little bit another behavior):



                                                                extension EnterConfirmationCodeTextField: UITextFieldDelegate 

                                                                func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
                                                                guard let textFieldCount = textField.text?.count else return false

                                                                // Сlosure
                                                                let setValueAndMoveForward =
                                                                textField.text = string
                                                                let nextTag = textField.tag + 1
                                                                if let nextResponder = textField.superview?.viewWithTag(nextTag)
                                                                nextResponder.becomeFirstResponder()



                                                                // Сlosure
                                                                let clearValueAndMoveBack =
                                                                textField.text = ""
                                                                let previousTag = textField.tag - 1
                                                                if let previousResponder = textField.superview?.viewWithTag(previousTag)
                                                                previousResponder.becomeFirstResponder()



                                                                if textFieldCount < 1 && string.count > 0

                                                                setValueAndMoveForward()

                                                                if textField.tag == 4
                                                                print("Do something")


                                                                return false

                                                                else if textFieldCount >= 1 && string.count == 0

                                                                clearValueAndMoveBack()
                                                                return false

                                                                else if textFieldCount >= 1

                                                                setValueAndMoveForward()
                                                                return false


                                                                return true






                                                                Also, I implemented this feature:



                                                                enter image description here



                                                                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



                                                                class EnterConfirmationCodeTextField: UITextField 

                                                                // MARK: Life cycle

                                                                override func awakeFromNib()
                                                                super.awakeFromNib()

                                                                delegate = self


                                                                // MARK: Methods

                                                                override func deleteBackward()
                                                                super.deleteBackward()

                                                                let previousTag = self.tag - 1
                                                                if let previousResponder = self.superview?.viewWithTag(previousTag)
                                                                previousResponder.becomeFirstResponder()

                                                                if let activeTextField = previousResponder as? UITextField
                                                                if let isEmpty = activeTextField.text?.isEmpty, !isEmpty
                                                                activeTextField.text = String()








                                                                Variant B (a little bit another behavior):



                                                                class EnterConfirmationCodeTextField: UITextField 

                                                                // MARK: Life cycle

                                                                override func awakeFromNib()
                                                                super.awakeFromNib()

                                                                delegate = self


                                                                // MARK: Methods

                                                                override func deleteBackward()
                                                                super.deleteBackward()

                                                                let previousTag = self.tag - 1
                                                                if let previousResponder = self.superview?.viewWithTag(previousTag)
                                                                previousResponder.becomeFirstResponder()






                                                                Assign EnterConfirmationCodeTextField for each of your textFields and set they appropriate tag value.






                                                                share|improve this answer
























                                                                  up vote
                                                                  1
                                                                  down vote










                                                                  up vote
                                                                  1
                                                                  down vote









                                                                  Swift 4



                                                                  Inspired by @Anurag Soni and @Varun Naharia answers



                                                                  Variant A



                                                                  extension EnterConfirmationCodeTextField: UITextFieldDelegate 

                                                                  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
                                                                  guard let textFieldCount = textField.text?.count else return false

                                                                  // Сlosure
                                                                  let setValueAndMoveForward =
                                                                  textField.text = string
                                                                  let nextTag = textField.tag + 1
                                                                  if let nextResponder = textField.superview?.viewWithTag(nextTag)
                                                                  nextResponder.becomeFirstResponder()



                                                                  // Сlosure
                                                                  let clearValueAndMoveBack =
                                                                  textField.text = ""
                                                                  let previousTag = textField.tag - 1
                                                                  if let previousResponder = textField.superview?.viewWithTag(previousTag)
                                                                  previousResponder.becomeFirstResponder()



                                                                  if textFieldCount < 1 && string.count > 0

                                                                  setValueAndMoveForward()

                                                                  if textField.tag == 4
                                                                  print("Do something")


                                                                  return false

                                                                  else if textFieldCount >= 1 && string.count == 0

                                                                  clearValueAndMoveBack()
                                                                  return false

                                                                  else if textFieldCount >= 1 && string.count > 0

                                                                  let nextTag = self.tag + 1
                                                                  if let previousResponder = self.superview?.viewWithTag(nextTag)
                                                                  previousResponder.becomeFirstResponder()

                                                                  if let activeTextField = previousResponder as? UITextField
                                                                  activeTextField.text = string



                                                                  return false


                                                                  return true






                                                                  Variant B (a little bit another behavior):



                                                                  extension EnterConfirmationCodeTextField: UITextFieldDelegate 

                                                                  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
                                                                  guard let textFieldCount = textField.text?.count else return false

                                                                  // Сlosure
                                                                  let setValueAndMoveForward =
                                                                  textField.text = string
                                                                  let nextTag = textField.tag + 1
                                                                  if let nextResponder = textField.superview?.viewWithTag(nextTag)
                                                                  nextResponder.becomeFirstResponder()



                                                                  // Сlosure
                                                                  let clearValueAndMoveBack =
                                                                  textField.text = ""
                                                                  let previousTag = textField.tag - 1
                                                                  if let previousResponder = textField.superview?.viewWithTag(previousTag)
                                                                  previousResponder.becomeFirstResponder()



                                                                  if textFieldCount < 1 && string.count > 0

                                                                  setValueAndMoveForward()

                                                                  if textField.tag == 4
                                                                  print("Do something")


                                                                  return false

                                                                  else if textFieldCount >= 1 && string.count == 0

                                                                  clearValueAndMoveBack()
                                                                  return false

                                                                  else if textFieldCount >= 1

                                                                  setValueAndMoveForward()
                                                                  return false


                                                                  return true






                                                                  Also, I implemented this feature:



                                                                  enter image description here



                                                                  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



                                                                  class EnterConfirmationCodeTextField: UITextField 

                                                                  // MARK: Life cycle

                                                                  override func awakeFromNib()
                                                                  super.awakeFromNib()

                                                                  delegate = self


                                                                  // MARK: Methods

                                                                  override func deleteBackward()
                                                                  super.deleteBackward()

                                                                  let previousTag = self.tag - 1
                                                                  if let previousResponder = self.superview?.viewWithTag(previousTag)
                                                                  previousResponder.becomeFirstResponder()

                                                                  if let activeTextField = previousResponder as? UITextField
                                                                  if let isEmpty = activeTextField.text?.isEmpty, !isEmpty
                                                                  activeTextField.text = String()








                                                                  Variant B (a little bit another behavior):



                                                                  class EnterConfirmationCodeTextField: UITextField 

                                                                  // MARK: Life cycle

                                                                  override func awakeFromNib()
                                                                  super.awakeFromNib()

                                                                  delegate = self


                                                                  // MARK: Methods

                                                                  override func deleteBackward()
                                                                  super.deleteBackward()

                                                                  let previousTag = self.tag - 1
                                                                  if let previousResponder = self.superview?.viewWithTag(previousTag)
                                                                  previousResponder.becomeFirstResponder()






                                                                  Assign EnterConfirmationCodeTextField for each of your textFields and set they appropriate tag value.






                                                                  share|improve this answer














                                                                  Swift 4



                                                                  Inspired by @Anurag Soni and @Varun Naharia answers



                                                                  Variant A



                                                                  extension EnterConfirmationCodeTextField: UITextFieldDelegate 

                                                                  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
                                                                  guard let textFieldCount = textField.text?.count else return false

                                                                  // Сlosure
                                                                  let setValueAndMoveForward =
                                                                  textField.text = string
                                                                  let nextTag = textField.tag + 1
                                                                  if let nextResponder = textField.superview?.viewWithTag(nextTag)
                                                                  nextResponder.becomeFirstResponder()



                                                                  // Сlosure
                                                                  let clearValueAndMoveBack =
                                                                  textField.text = ""
                                                                  let previousTag = textField.tag - 1
                                                                  if let previousResponder = textField.superview?.viewWithTag(previousTag)
                                                                  previousResponder.becomeFirstResponder()



                                                                  if textFieldCount < 1 && string.count > 0

                                                                  setValueAndMoveForward()

                                                                  if textField.tag == 4
                                                                  print("Do something")


                                                                  return false

                                                                  else if textFieldCount >= 1 && string.count == 0

                                                                  clearValueAndMoveBack()
                                                                  return false

                                                                  else if textFieldCount >= 1 && string.count > 0

                                                                  let nextTag = self.tag + 1
                                                                  if let previousResponder = self.superview?.viewWithTag(nextTag)
                                                                  previousResponder.becomeFirstResponder()

                                                                  if let activeTextField = previousResponder as? UITextField
                                                                  activeTextField.text = string



                                                                  return false


                                                                  return true






                                                                  Variant B (a little bit another behavior):



                                                                  extension EnterConfirmationCodeTextField: UITextFieldDelegate 

                                                                  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
                                                                  guard let textFieldCount = textField.text?.count else return false

                                                                  // Сlosure
                                                                  let setValueAndMoveForward =
                                                                  textField.text = string
                                                                  let nextTag = textField.tag + 1
                                                                  if let nextResponder = textField.superview?.viewWithTag(nextTag)
                                                                  nextResponder.becomeFirstResponder()



                                                                  // Сlosure
                                                                  let clearValueAndMoveBack =
                                                                  textField.text = ""
                                                                  let previousTag = textField.tag - 1
                                                                  if let previousResponder = textField.superview?.viewWithTag(previousTag)
                                                                  previousResponder.becomeFirstResponder()



                                                                  if textFieldCount < 1 && string.count > 0

                                                                  setValueAndMoveForward()

                                                                  if textField.tag == 4
                                                                  print("Do something")


                                                                  return false

                                                                  else if textFieldCount >= 1 && string.count == 0

                                                                  clearValueAndMoveBack()
                                                                  return false

                                                                  else if textFieldCount >= 1

                                                                  setValueAndMoveForward()
                                                                  return false


                                                                  return true






                                                                  Also, I implemented this feature:



                                                                  enter image description here



                                                                  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



                                                                  class EnterConfirmationCodeTextField: UITextField 

                                                                  // MARK: Life cycle

                                                                  override func awakeFromNib()
                                                                  super.awakeFromNib()

                                                                  delegate = self


                                                                  // MARK: Methods

                                                                  override func deleteBackward()
                                                                  super.deleteBackward()

                                                                  let previousTag = self.tag - 1
                                                                  if let previousResponder = self.superview?.viewWithTag(previousTag)
                                                                  previousResponder.becomeFirstResponder()

                                                                  if let activeTextField = previousResponder as? UITextField
                                                                  if let isEmpty = activeTextField.text?.isEmpty, !isEmpty
                                                                  activeTextField.text = String()








                                                                  Variant B (a little bit another behavior):



                                                                  class EnterConfirmationCodeTextField: UITextField 

                                                                  // MARK: Life cycle

                                                                  override func awakeFromNib()
                                                                  super.awakeFromNib()

                                                                  delegate = self


                                                                  // MARK: Methods

                                                                  override func deleteBackward()
                                                                  super.deleteBackward()

                                                                  let previousTag = self.tag - 1
                                                                  if let previousResponder = self.superview?.viewWithTag(previousTag)
                                                                  previousResponder.becomeFirstResponder()






                                                                  Assign EnterConfirmationCodeTextField for each of your textFields and set they appropriate tag value.







                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Aug 30 at 17:58

























                                                                  answered Aug 30 at 16:02









                                                                  Roman Romanenko

                                                                  349415




                                                                  349415




















                                                                      up vote
                                                                      1
                                                                      down vote













                                                                      enter image description here



                                                                      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



                                                                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                      guard let textField = textField as? PinCodeTextField else return true

                                                                      if string == "" // when the backward clicked, let it go :)
                                                                      return true


                                                                      // when textfield is not empty, well, next number
                                                                      if textField.pinCode.count == textField.maxCount
                                                                      becomeFirstResponder(after: textField)
                                                                      return false


                                                                      if string.count > textField.maxCharacterCount // the max character count should be 1
                                                                      return false

                                                                      return true

                                                                      // now the text field has been filled with a number
                                                                      func textFieldCotentDidChange(_ textField: UITextField)
                                                                      print("didchange")
                                                                      guard let textField = textField as? PinCodeTextField else return
                                                                      if textField.pinCode.count == 0
                                                                      becomeFirstResponder(before: textField)


                                                                      // when textfield has been filled, ok! next!
                                                                      if textField.pinCode.count == textField.maxCharacterCount
                                                                      becomeFirstResponder(after: textField)




                                                                      for more details and the simple demo, see this link






                                                                      share|improve this answer


























                                                                        up vote
                                                                        1
                                                                        down vote













                                                                        enter image description here



                                                                        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



                                                                        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                        guard let textField = textField as? PinCodeTextField else return true

                                                                        if string == "" // when the backward clicked, let it go :)
                                                                        return true


                                                                        // when textfield is not empty, well, next number
                                                                        if textField.pinCode.count == textField.maxCount
                                                                        becomeFirstResponder(after: textField)
                                                                        return false


                                                                        if string.count > textField.maxCharacterCount // the max character count should be 1
                                                                        return false

                                                                        return true

                                                                        // now the text field has been filled with a number
                                                                        func textFieldCotentDidChange(_ textField: UITextField)
                                                                        print("didchange")
                                                                        guard let textField = textField as? PinCodeTextField else return
                                                                        if textField.pinCode.count == 0
                                                                        becomeFirstResponder(before: textField)


                                                                        // when textfield has been filled, ok! next!
                                                                        if textField.pinCode.count == textField.maxCharacterCount
                                                                        becomeFirstResponder(after: textField)




                                                                        for more details and the simple demo, see this link






                                                                        share|improve this answer
























                                                                          up vote
                                                                          1
                                                                          down vote










                                                                          up vote
                                                                          1
                                                                          down vote









                                                                          enter image description here



                                                                          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



                                                                          func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                          guard let textField = textField as? PinCodeTextField else return true

                                                                          if string == "" // when the backward clicked, let it go :)
                                                                          return true


                                                                          // when textfield is not empty, well, next number
                                                                          if textField.pinCode.count == textField.maxCount
                                                                          becomeFirstResponder(after: textField)
                                                                          return false


                                                                          if string.count > textField.maxCharacterCount // the max character count should be 1
                                                                          return false

                                                                          return true

                                                                          // now the text field has been filled with a number
                                                                          func textFieldCotentDidChange(_ textField: UITextField)
                                                                          print("didchange")
                                                                          guard let textField = textField as? PinCodeTextField else return
                                                                          if textField.pinCode.count == 0
                                                                          becomeFirstResponder(before: textField)


                                                                          // when textfield has been filled, ok! next!
                                                                          if textField.pinCode.count == textField.maxCharacterCount
                                                                          becomeFirstResponder(after: textField)




                                                                          for more details and the simple demo, see this link






                                                                          share|improve this answer














                                                                          enter image description here



                                                                          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



                                                                          func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                          guard let textField = textField as? PinCodeTextField else return true

                                                                          if string == "" // when the backward clicked, let it go :)
                                                                          return true


                                                                          // when textfield is not empty, well, next number
                                                                          if textField.pinCode.count == textField.maxCount
                                                                          becomeFirstResponder(after: textField)
                                                                          return false


                                                                          if string.count > textField.maxCharacterCount // the max character count should be 1
                                                                          return false

                                                                          return true

                                                                          // now the text field has been filled with a number
                                                                          func textFieldCotentDidChange(_ textField: UITextField)
                                                                          print("didchange")
                                                                          guard let textField = textField as? PinCodeTextField else return
                                                                          if textField.pinCode.count == 0
                                                                          becomeFirstResponder(before: textField)


                                                                          // when textfield has been filled, ok! next!
                                                                          if textField.pinCode.count == textField.maxCharacterCount
                                                                          becomeFirstResponder(after: textField)




                                                                          for more details and the simple demo, see this link







                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited Nov 10 at 14:18

























                                                                          answered Nov 10 at 14:03









                                                                          Jiangshi Fresh

                                                                          612




                                                                          612




















                                                                              up vote
                                                                              0
                                                                              down vote













                                                                              Just use TextFieldDelegate method and check the length of the TextField after every changes



                                                                              func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                                                                              let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
                                                                              if newString.characters.count == 1

                                                                              nextTextField.becomeFirstResponder()
                                                                              return true

                                                                              else

                                                                              return false







                                                                              share|improve this answer
























                                                                                up vote
                                                                                0
                                                                                down vote













                                                                                Just use TextFieldDelegate method and check the length of the TextField after every changes



                                                                                func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                                                                                let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
                                                                                if newString.characters.count == 1

                                                                                nextTextField.becomeFirstResponder()
                                                                                return true

                                                                                else

                                                                                return false







                                                                                share|improve this answer






















                                                                                  up vote
                                                                                  0
                                                                                  down vote










                                                                                  up vote
                                                                                  0
                                                                                  down vote









                                                                                  Just use TextFieldDelegate method and check the length of the TextField after every changes



                                                                                  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                                                                                  let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
                                                                                  if newString.characters.count == 1

                                                                                  nextTextField.becomeFirstResponder()
                                                                                  return true

                                                                                  else

                                                                                  return false







                                                                                  share|improve this answer












                                                                                  Just use TextFieldDelegate method and check the length of the TextField after every changes



                                                                                  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
                                                                                  let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
                                                                                  if newString.characters.count == 1

                                                                                  nextTextField.becomeFirstResponder()
                                                                                  return true

                                                                                  else

                                                                                  return false








                                                                                  share|improve this answer












                                                                                  share|improve this answer



                                                                                  share|improve this answer










                                                                                  answered Jul 1 '16 at 7:04









                                                                                  Amit Singh

                                                                                  2,1061445




                                                                                  2,1061445




















                                                                                      up vote
                                                                                      0
                                                                                      down vote













                                                                                      Modified Anurag Soni's answer in Swift 3.



                                                                                      • It assumes you have outlet collection named textFields and the text fields have ordered tags set

                                                                                      • It adds case when there's already some digit in text field and when user types something new - the digit is replaced

                                                                                      • Input is restricted to digits only


                                                                                      • It prevents from pasting more than one digit



                                                                                        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                                        // Restrict to only digits
                                                                                        let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
                                                                                        let compSepByCharInSet = string.components(separatedBy: aSet)
                                                                                        let numberFiltered = compSepByCharInSet.joined(separator: "")

                                                                                        if string != numberFiltered
                                                                                        return false


                                                                                        // Get the unwrapped text
                                                                                        guard let text = textField.text else
                                                                                        return false


                                                                                        if (text.characters.count < 1 && string.characters.count == 1)
                                                                                        // New value to empty text field
                                                                                        textField.text = string

                                                                                        // Next responder
                                                                                        if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                                                                                        someTextField.becomeFirstResponder()
                                                                                        else
                                                                                        view.endEditing(true)


                                                                                        return false
                                                                                        else if (text.characters.count >= 1 && string.characters.count == 0)
                                                                                        // On deleting value from Textfield
                                                                                        textField.text = ""

                                                                                        // Previous responder
                                                                                        if let someTextField = (textFields.filter $0.tag == textField.tag - 1 ).first
                                                                                        someTextField.becomeFirstResponder()
                                                                                        else
                                                                                        view.endEditing(true)


                                                                                        return false
                                                                                        else if string.characters.count == 1
                                                                                        // There's already some digit in text field
                                                                                        // Replace it with new one
                                                                                        textField.text = string

                                                                                        // Next responder
                                                                                        if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                                                                                        someTextField.becomeFirstResponder()
                                                                                        else
                                                                                        view.endEditing(true)



                                                                                        return false







                                                                                      share|improve this answer
























                                                                                        up vote
                                                                                        0
                                                                                        down vote













                                                                                        Modified Anurag Soni's answer in Swift 3.



                                                                                        • It assumes you have outlet collection named textFields and the text fields have ordered tags set

                                                                                        • It adds case when there's already some digit in text field and when user types something new - the digit is replaced

                                                                                        • Input is restricted to digits only


                                                                                        • It prevents from pasting more than one digit



                                                                                          func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                                          // Restrict to only digits
                                                                                          let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
                                                                                          let compSepByCharInSet = string.components(separatedBy: aSet)
                                                                                          let numberFiltered = compSepByCharInSet.joined(separator: "")

                                                                                          if string != numberFiltered
                                                                                          return false


                                                                                          // Get the unwrapped text
                                                                                          guard let text = textField.text else
                                                                                          return false


                                                                                          if (text.characters.count < 1 && string.characters.count == 1)
                                                                                          // New value to empty text field
                                                                                          textField.text = string

                                                                                          // Next responder
                                                                                          if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                                                                                          someTextField.becomeFirstResponder()
                                                                                          else
                                                                                          view.endEditing(true)


                                                                                          return false
                                                                                          else if (text.characters.count >= 1 && string.characters.count == 0)
                                                                                          // On deleting value from Textfield
                                                                                          textField.text = ""

                                                                                          // Previous responder
                                                                                          if let someTextField = (textFields.filter $0.tag == textField.tag - 1 ).first
                                                                                          someTextField.becomeFirstResponder()
                                                                                          else
                                                                                          view.endEditing(true)


                                                                                          return false
                                                                                          else if string.characters.count == 1
                                                                                          // There's already some digit in text field
                                                                                          // Replace it with new one
                                                                                          textField.text = string

                                                                                          // Next responder
                                                                                          if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                                                                                          someTextField.becomeFirstResponder()
                                                                                          else
                                                                                          view.endEditing(true)



                                                                                          return false







                                                                                        share|improve this answer






















                                                                                          up vote
                                                                                          0
                                                                                          down vote










                                                                                          up vote
                                                                                          0
                                                                                          down vote









                                                                                          Modified Anurag Soni's answer in Swift 3.



                                                                                          • It assumes you have outlet collection named textFields and the text fields have ordered tags set

                                                                                          • It adds case when there's already some digit in text field and when user types something new - the digit is replaced

                                                                                          • Input is restricted to digits only


                                                                                          • It prevents from pasting more than one digit



                                                                                            func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                                            // Restrict to only digits
                                                                                            let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
                                                                                            let compSepByCharInSet = string.components(separatedBy: aSet)
                                                                                            let numberFiltered = compSepByCharInSet.joined(separator: "")

                                                                                            if string != numberFiltered
                                                                                            return false


                                                                                            // Get the unwrapped text
                                                                                            guard let text = textField.text else
                                                                                            return false


                                                                                            if (text.characters.count < 1 && string.characters.count == 1)
                                                                                            // New value to empty text field
                                                                                            textField.text = string

                                                                                            // Next responder
                                                                                            if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                                                                                            someTextField.becomeFirstResponder()
                                                                                            else
                                                                                            view.endEditing(true)


                                                                                            return false
                                                                                            else if (text.characters.count >= 1 && string.characters.count == 0)
                                                                                            // On deleting value from Textfield
                                                                                            textField.text = ""

                                                                                            // Previous responder
                                                                                            if let someTextField = (textFields.filter $0.tag == textField.tag - 1 ).first
                                                                                            someTextField.becomeFirstResponder()
                                                                                            else
                                                                                            view.endEditing(true)


                                                                                            return false
                                                                                            else if string.characters.count == 1
                                                                                            // There's already some digit in text field
                                                                                            // Replace it with new one
                                                                                            textField.text = string

                                                                                            // Next responder
                                                                                            if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                                                                                            someTextField.becomeFirstResponder()
                                                                                            else
                                                                                            view.endEditing(true)



                                                                                            return false







                                                                                          share|improve this answer












                                                                                          Modified Anurag Soni's answer in Swift 3.



                                                                                          • It assumes you have outlet collection named textFields and the text fields have ordered tags set

                                                                                          • It adds case when there's already some digit in text field and when user types something new - the digit is replaced

                                                                                          • Input is restricted to digits only


                                                                                          • It prevents from pasting more than one digit



                                                                                            func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                                            // Restrict to only digits
                                                                                            let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
                                                                                            let compSepByCharInSet = string.components(separatedBy: aSet)
                                                                                            let numberFiltered = compSepByCharInSet.joined(separator: "")

                                                                                            if string != numberFiltered
                                                                                            return false


                                                                                            // Get the unwrapped text
                                                                                            guard let text = textField.text else
                                                                                            return false


                                                                                            if (text.characters.count < 1 && string.characters.count == 1)
                                                                                            // New value to empty text field
                                                                                            textField.text = string

                                                                                            // Next responder
                                                                                            if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                                                                                            someTextField.becomeFirstResponder()
                                                                                            else
                                                                                            view.endEditing(true)


                                                                                            return false
                                                                                            else if (text.characters.count >= 1 && string.characters.count == 0)
                                                                                            // On deleting value from Textfield
                                                                                            textField.text = ""

                                                                                            // Previous responder
                                                                                            if let someTextField = (textFields.filter $0.tag == textField.tag - 1 ).first
                                                                                            someTextField.becomeFirstResponder()
                                                                                            else
                                                                                            view.endEditing(true)


                                                                                            return false
                                                                                            else if string.characters.count == 1
                                                                                            // There's already some digit in text field
                                                                                            // Replace it with new one
                                                                                            textField.text = string

                                                                                            // Next responder
                                                                                            if let someTextField = (textFields.filter $0.tag == textField.tag + 1 ).first
                                                                                            someTextField.becomeFirstResponder()
                                                                                            else
                                                                                            view.endEditing(true)



                                                                                            return false








                                                                                          share|improve this answer












                                                                                          share|improve this answer



                                                                                          share|improve this answer










                                                                                          answered Oct 5 '17 at 11:02









                                                                                          jonaszmclaren

                                                                                          1,5871420




                                                                                          1,5871420




















                                                                                              up vote
                                                                                              0
                                                                                              down vote













                                                                                              I was working on a similar functionality and did it in my way. Solution below.
                                                                                              Swift 4



                                                                                              func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

                                                                                              //For clear button pressed
                                                                                              //If the textfield has already text in it
                                                                                              if string.count == 0
                                                                                              textField.text = string
                                                                                              return true


                                                                                              //For First time entry into the text field
                                                                                              guard let text = textField.text, text.count <= 0 else
                                                                                              //If user enter second character
                                                                                              return false

                                                                                              //For First time entry into the text field
                                                                                              if text.count == 0
                                                                                              textField.text = string
                                                                                              textField.resignFirstResponder()
                                                                                              self.nextResponde(tag: textField.tag)
                                                                                              return true

                                                                                              return false


                                                                                              //To make the next field as responder
                                                                                              func nextResponde(tag: Int)
                                                                                              switch tag
                                                                                              case self.PINTextField.tag:
                                                                                              guard let text = self.PINTextField1.text, text.count == 1 else
                                                                                              self.PINTextField1.becomeFirstResponder()
                                                                                              return

                                                                                              case self.PINTextField1.tag:
                                                                                              guard let text = self.PINTextField2.text, text.count == 1 else
                                                                                              self.PINTextField2.becomeFirstResponder()
                                                                                              return

                                                                                              case self.PINTextField2.tag:
                                                                                              guard let text = self.PINTextField3.text, text.count == 1 else
                                                                                              self.PINTextField3.becomeFirstResponder()
                                                                                              return

                                                                                              default:
                                                                                              let _ = tag







                                                                                              share|improve this answer
























                                                                                                up vote
                                                                                                0
                                                                                                down vote













                                                                                                I was working on a similar functionality and did it in my way. Solution below.
                                                                                                Swift 4



                                                                                                func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

                                                                                                //For clear button pressed
                                                                                                //If the textfield has already text in it
                                                                                                if string.count == 0
                                                                                                textField.text = string
                                                                                                return true


                                                                                                //For First time entry into the text field
                                                                                                guard let text = textField.text, text.count <= 0 else
                                                                                                //If user enter second character
                                                                                                return false

                                                                                                //For First time entry into the text field
                                                                                                if text.count == 0
                                                                                                textField.text = string
                                                                                                textField.resignFirstResponder()
                                                                                                self.nextResponde(tag: textField.tag)
                                                                                                return true

                                                                                                return false


                                                                                                //To make the next field as responder
                                                                                                func nextResponde(tag: Int)
                                                                                                switch tag
                                                                                                case self.PINTextField.tag:
                                                                                                guard let text = self.PINTextField1.text, text.count == 1 else
                                                                                                self.PINTextField1.becomeFirstResponder()
                                                                                                return

                                                                                                case self.PINTextField1.tag:
                                                                                                guard let text = self.PINTextField2.text, text.count == 1 else
                                                                                                self.PINTextField2.becomeFirstResponder()
                                                                                                return

                                                                                                case self.PINTextField2.tag:
                                                                                                guard let text = self.PINTextField3.text, text.count == 1 else
                                                                                                self.PINTextField3.becomeFirstResponder()
                                                                                                return

                                                                                                default:
                                                                                                let _ = tag







                                                                                                share|improve this answer






















                                                                                                  up vote
                                                                                                  0
                                                                                                  down vote










                                                                                                  up vote
                                                                                                  0
                                                                                                  down vote









                                                                                                  I was working on a similar functionality and did it in my way. Solution below.
                                                                                                  Swift 4



                                                                                                  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

                                                                                                  //For clear button pressed
                                                                                                  //If the textfield has already text in it
                                                                                                  if string.count == 0
                                                                                                  textField.text = string
                                                                                                  return true


                                                                                                  //For First time entry into the text field
                                                                                                  guard let text = textField.text, text.count <= 0 else
                                                                                                  //If user enter second character
                                                                                                  return false

                                                                                                  //For First time entry into the text field
                                                                                                  if text.count == 0
                                                                                                  textField.text = string
                                                                                                  textField.resignFirstResponder()
                                                                                                  self.nextResponde(tag: textField.tag)
                                                                                                  return true

                                                                                                  return false


                                                                                                  //To make the next field as responder
                                                                                                  func nextResponde(tag: Int)
                                                                                                  switch tag
                                                                                                  case self.PINTextField.tag:
                                                                                                  guard let text = self.PINTextField1.text, text.count == 1 else
                                                                                                  self.PINTextField1.becomeFirstResponder()
                                                                                                  return

                                                                                                  case self.PINTextField1.tag:
                                                                                                  guard let text = self.PINTextField2.text, text.count == 1 else
                                                                                                  self.PINTextField2.becomeFirstResponder()
                                                                                                  return

                                                                                                  case self.PINTextField2.tag:
                                                                                                  guard let text = self.PINTextField3.text, text.count == 1 else
                                                                                                  self.PINTextField3.becomeFirstResponder()
                                                                                                  return

                                                                                                  default:
                                                                                                  let _ = tag







                                                                                                  share|improve this answer












                                                                                                  I was working on a similar functionality and did it in my way. Solution below.
                                                                                                  Swift 4



                                                                                                  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 

                                                                                                  //For clear button pressed
                                                                                                  //If the textfield has already text in it
                                                                                                  if string.count == 0
                                                                                                  textField.text = string
                                                                                                  return true


                                                                                                  //For First time entry into the text field
                                                                                                  guard let text = textField.text, text.count <= 0 else
                                                                                                  //If user enter second character
                                                                                                  return false

                                                                                                  //For First time entry into the text field
                                                                                                  if text.count == 0
                                                                                                  textField.text = string
                                                                                                  textField.resignFirstResponder()
                                                                                                  self.nextResponde(tag: textField.tag)
                                                                                                  return true

                                                                                                  return false


                                                                                                  //To make the next field as responder
                                                                                                  func nextResponde(tag: Int)
                                                                                                  switch tag
                                                                                                  case self.PINTextField.tag:
                                                                                                  guard let text = self.PINTextField1.text, text.count == 1 else
                                                                                                  self.PINTextField1.becomeFirstResponder()
                                                                                                  return

                                                                                                  case self.PINTextField1.tag:
                                                                                                  guard let text = self.PINTextField2.text, text.count == 1 else
                                                                                                  self.PINTextField2.becomeFirstResponder()
                                                                                                  return

                                                                                                  case self.PINTextField2.tag:
                                                                                                  guard let text = self.PINTextField3.text, text.count == 1 else
                                                                                                  self.PINTextField3.becomeFirstResponder()
                                                                                                  return

                                                                                                  default:
                                                                                                  let _ = tag








                                                                                                  share|improve this answer












                                                                                                  share|improve this answer



                                                                                                  share|improve this answer










                                                                                                  answered Oct 24 at 10:22









                                                                                                  Logunath

                                                                                                  2721617




                                                                                                  2721617




















                                                                                                      up vote
                                                                                                      -1
                                                                                                      down vote













                                                                                                      try this : - For swift 3.0



                                                                                                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                                                      // On inputing value to textfield
                                                                                                      if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
                                                                                                      let nextTag = textField.tag + 1;

                                                                                                      // get next responder
                                                                                                      let nextResponder = textField.superview?.viewWithTag(nextTag);
                                                                                                      textField.text = string;

                                                                                                      if (nextResponder == nil)
                                                                                                      textField.resignFirstResponder()

                                                                                                      nextResponder?.becomeFirstResponder();
                                                                                                      return false;

                                                                                                      else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
                                                                                                      // on deleting value from Textfield
                                                                                                      let previousTag = textField.tag - 1;

                                                                                                      // get next responder
                                                                                                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                                                                                                      if (previousResponder == nil)
                                                                                                      previousResponder = textField.superview?.viewWithTag(1);

                                                                                                      textField.text = "";
                                                                                                      previousResponder?.becomeFirstResponder();
                                                                                                      return false;

                                                                                                      return true;



                                                                                                      converted Anurag Soni answer in swift 3.0



                                                                                                      You just have to implement this method only.






                                                                                                      share|improve this answer




















                                                                                                      • Anurag Soni's answer is completely wrong. When user tried to edit textfield again it will allow them to enter as many character as they can.
                                                                                                        – Nij
                                                                                                        Jul 12 '17 at 10:25










                                                                                                      • @Nij I tested it and it was working fine here. And I only converted it to Swift 3.
                                                                                                        – Sachin Nautiyal
                                                                                                        Jul 17 '17 at 8:41














                                                                                                      up vote
                                                                                                      -1
                                                                                                      down vote













                                                                                                      try this : - For swift 3.0



                                                                                                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                                                      // On inputing value to textfield
                                                                                                      if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
                                                                                                      let nextTag = textField.tag + 1;

                                                                                                      // get next responder
                                                                                                      let nextResponder = textField.superview?.viewWithTag(nextTag);
                                                                                                      textField.text = string;

                                                                                                      if (nextResponder == nil)
                                                                                                      textField.resignFirstResponder()

                                                                                                      nextResponder?.becomeFirstResponder();
                                                                                                      return false;

                                                                                                      else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
                                                                                                      // on deleting value from Textfield
                                                                                                      let previousTag = textField.tag - 1;

                                                                                                      // get next responder
                                                                                                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                                                                                                      if (previousResponder == nil)
                                                                                                      previousResponder = textField.superview?.viewWithTag(1);

                                                                                                      textField.text = "";
                                                                                                      previousResponder?.becomeFirstResponder();
                                                                                                      return false;

                                                                                                      return true;



                                                                                                      converted Anurag Soni answer in swift 3.0



                                                                                                      You just have to implement this method only.






                                                                                                      share|improve this answer




















                                                                                                      • Anurag Soni's answer is completely wrong. When user tried to edit textfield again it will allow them to enter as many character as they can.
                                                                                                        – Nij
                                                                                                        Jul 12 '17 at 10:25










                                                                                                      • @Nij I tested it and it was working fine here. And I only converted it to Swift 3.
                                                                                                        – Sachin Nautiyal
                                                                                                        Jul 17 '17 at 8:41












                                                                                                      up vote
                                                                                                      -1
                                                                                                      down vote










                                                                                                      up vote
                                                                                                      -1
                                                                                                      down vote









                                                                                                      try this : - For swift 3.0



                                                                                                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                                                      // On inputing value to textfield
                                                                                                      if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
                                                                                                      let nextTag = textField.tag + 1;

                                                                                                      // get next responder
                                                                                                      let nextResponder = textField.superview?.viewWithTag(nextTag);
                                                                                                      textField.text = string;

                                                                                                      if (nextResponder == nil)
                                                                                                      textField.resignFirstResponder()

                                                                                                      nextResponder?.becomeFirstResponder();
                                                                                                      return false;

                                                                                                      else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
                                                                                                      // on deleting value from Textfield
                                                                                                      let previousTag = textField.tag - 1;

                                                                                                      // get next responder
                                                                                                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                                                                                                      if (previousResponder == nil)
                                                                                                      previousResponder = textField.superview?.viewWithTag(1);

                                                                                                      textField.text = "";
                                                                                                      previousResponder?.becomeFirstResponder();
                                                                                                      return false;

                                                                                                      return true;



                                                                                                      converted Anurag Soni answer in swift 3.0



                                                                                                      You just have to implement this method only.






                                                                                                      share|improve this answer












                                                                                                      try this : - For swift 3.0



                                                                                                      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
                                                                                                      // On inputing value to textfield
                                                                                                      if ((textField.text?.characters.count)! < 1 && string.characters.count > 0)
                                                                                                      let nextTag = textField.tag + 1;

                                                                                                      // get next responder
                                                                                                      let nextResponder = textField.superview?.viewWithTag(nextTag);
                                                                                                      textField.text = string;

                                                                                                      if (nextResponder == nil)
                                                                                                      textField.resignFirstResponder()

                                                                                                      nextResponder?.becomeFirstResponder();
                                                                                                      return false;

                                                                                                      else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0)
                                                                                                      // on deleting value from Textfield
                                                                                                      let previousTag = textField.tag - 1;

                                                                                                      // get next responder
                                                                                                      var previousResponder = textField.superview?.viewWithTag(previousTag);

                                                                                                      if (previousResponder == nil)
                                                                                                      previousResponder = textField.superview?.viewWithTag(1);

                                                                                                      textField.text = "";
                                                                                                      previousResponder?.becomeFirstResponder();
                                                                                                      return false;

                                                                                                      return true;



                                                                                                      converted Anurag Soni answer in swift 3.0



                                                                                                      You just have to implement this method only.







                                                                                                      share|improve this answer












                                                                                                      share|improve this answer



                                                                                                      share|improve this answer










                                                                                                      answered Jun 6 '17 at 12:31









                                                                                                      Sachin Nautiyal

                                                                                                      15212




                                                                                                      15212











                                                                                                      • Anurag Soni's answer is completely wrong. When user tried to edit textfield again it will allow them to enter as many character as they can.
                                                                                                        – Nij
                                                                                                        Jul 12 '17 at 10:25










                                                                                                      • @Nij I tested it and it was working fine here. And I only converted it to Swift 3.
                                                                                                        – Sachin Nautiyal
                                                                                                        Jul 17 '17 at 8:41
















                                                                                                      • Anurag Soni's answer is completely wrong. When user tried to edit textfield again it will allow them to enter as many character as they can.
                                                                                                        – Nij
                                                                                                        Jul 12 '17 at 10:25










                                                                                                      • @Nij I tested it and it was working fine here. And I only converted it to Swift 3.
                                                                                                        – Sachin Nautiyal
                                                                                                        Jul 17 '17 at 8:41















                                                                                                      Anurag Soni's answer is completely wrong. When user tried to edit textfield again it will allow them to enter as many character as they can.
                                                                                                      – Nij
                                                                                                      Jul 12 '17 at 10:25




                                                                                                      Anurag Soni's answer is completely wrong. When user tried to edit textfield again it will allow them to enter as many character as they can.
                                                                                                      – Nij
                                                                                                      Jul 12 '17 at 10:25












                                                                                                      @Nij I tested it and it was working fine here. And I only converted it to Swift 3.
                                                                                                      – Sachin Nautiyal
                                                                                                      Jul 17 '17 at 8:41




                                                                                                      @Nij I tested it and it was working fine here. And I only converted it to Swift 3.
                                                                                                      – Sachin Nautiyal
                                                                                                      Jul 17 '17 at 8:41

















                                                                                                       

                                                                                                      draft saved


                                                                                                      draft discarded















































                                                                                                       


                                                                                                      draft saved


                                                                                                      draft discarded














                                                                                                      StackExchange.ready(
                                                                                                      function ()
                                                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18711468%2fallowing-single-digit-in-uitextfield-in-ios%23new-answer', 'question_page');

                                                                                                      );

                                                                                                      Post as a guest















                                                                                                      Required, but never shown





















































                                                                                                      Required, but never shown














                                                                                                      Required, but never shown












                                                                                                      Required, but never shown







                                                                                                      Required, but never shown

































                                                                                                      Required, but never shown














                                                                                                      Required, but never shown












                                                                                                      Required, but never shown







                                                                                                      Required, but never shown







                                                                                                      Popular posts from this blog

                                                                                                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                                                                                                      政党

                                                                                                      天津地下鉄3号線