1

I have 6 text field text box. And when user finish enter in first text field, then it should automatically have to go second and so on. Here my code hats not working. when i enter the value in first text field, the values is entering in second ..here my code :

This below code i have done to enter only one digit in each text field. But here how can i add the text field responder...once i finish with first text field, adn so on...

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


        if textField == self.cardTextFieldOne{
        guard let text = textField.text else {return true}

        let newLength = text.characters.count + string.characters.count - range.length
        return newLength <= 1
        }else if textField == self.cardTextFieldTwo{
            guard let text = textField.text else { return true }

            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= 1
        }
        else if textField == self.cardTextFieldTwo{
            guard let text = textField.text else { return true }

            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= 1
        }
        else if textField == self.cardTextFieldThree{
            guard let text = textField.text else { return true }

            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= 1
        }
        else if textField == self.cardTextFieldFour{
            guard let text = textField.text else { return true }
            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= 1
        }
        else if textField == self.cardTextFieldFive{
            guard let text = textField.text else { return true }
            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= 1
        }
        else if textField == self.cardTextFieldSix{
            guard let text = textField.text else { return true }
            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= 1
        }
        else if textField == self.cardTextFieldMonth{
            guard let text = textField.text else { return true }
            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= 2
        }
        else if textField == self.cardTextFieldYear{
            guard let text = textField.text else { return true }
            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= 2
        }
        return true



    }

Thanks in advance !!

Update

 if textField == self.cardTextFieldOne{


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

            if  (string == "") || string.characters.count < 1
            {
                return true //This also allow you to backspace
            }else if  (cardTextFieldOne.text?.characters.count)! >= 1
            {
                cardTextFieldOne.resignFirstResponder();
                self.cardTextFieldTwo.becomeFirstResponder()

                return false
            }


            return newLength <= 1




        }
doubtman
  • 61
  • 13

4 Answers4

0

Try using textField's delegate method

   func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if textField == self.cardTextFieldOne{
        textField.resignFirstResponder();
        self.cardTextFieldTwo.becomeFirstResponder();
        return false;
    }else if textField == self.cardTextFieldTwo{
        textField.resignFirstResponder();
        self.cardTextFieldThree.becomeFirstResponder()
        // if this is last field then return true
        return true
    }
}
Vikky
  • 914
  • 1
  • 6
  • 16
  • any solution for here https://stackoverflow.com/questions/45667948/how-to-add-followed-text-to-uitextfield – doubtman Aug 14 '17 at 10:25
0

Try to respond manually while textfield fill with single text.

if textField == self.cardTextFieldTwo{
            guard let text = textField.text else { return true }

            let newLength = text.characters.count + string.characters.count - range.length
            if textField.text?.utf16.count==1 { // adjust your condition here
              textField.resignFirstResponder()
              self.cardTextFieldTwo.becomeFirstResponder()
            }

            return newLength <= 1
        }

Note do this for all textfield

KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • i tried ur, buts once i enter one digit and if i try to type second digit , then only its moving to next text field box – doubtman Jul 28 '17 at 09:33
  • did you check updated answer ? if yes then make changes in condition and check again . because i cant debug issue so. – KKRocks Jul 28 '17 at 09:39
  • i tried all condition, even ur new solution..the also once i enter 2nd digit in first text field then only its going to 2nd text field box.....count >=1 , count ==1...all tried – doubtman Jul 28 '17 at 09:58
  • try this to check length : https://stackoverflow.com/a/30656616/3901620 – KKRocks Jul 28 '17 at 10:01
  • any solution for here https://stackoverflow.com/questions/45667948/how-to-add-followed-text-to-uitextfield – doubtman Aug 14 '17 at 10:26
0

Use textFieldDidEndEditing method, than just call becomeFirstResponder() on next textField

func textFieldDidEndEditing(_ textField: UITextField) {
    textField.becomeFirstResponder()
}
Temirlan
  • 162
  • 1
  • 7
  • any solution for here https://stackoverflow.com/questions/45667948/how-to-add-followed-text-to-uitextfield – doubtman Aug 14 '17 at 10:26
0

Simple Solution

Create Method which called every time you change the textField text like below you can see in the Image, while you right click on textfield, you see the Editing change drag it and create method like enter image description here

 @IBAction func editing(_ sender: UITextField)
    {
        if sender == yourFirstTextField {
            if sender.text?.characters.count == 1 {
                yourNextTextField.becomeFirstResponder()
            }
        }

    }
Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
  • but as per my post code..i need to integrate ur code to... but i dont know how to integrate both... – doubtman Jul 28 '17 at 12:05
  • Just check for first text field – Jaydeep Vyas Jul 28 '17 at 12:15
  • as per my solution you can only allow to type one character in first textField and it focus to second textfield what is your output with updated code – Jaydeep Vyas Jul 28 '17 at 12:22
  • okay..but when i try ur code also , when i enter first value in first text field, if i enter 2nd digit (ist not showing in first text field) then its moving to secons screen... – doubtman Jul 28 '17 at 12:26
  • Okey than as per my solution is work perfect, now what you want – Jaydeep Vyas Jul 28 '17 at 12:28
  • i want to enter only one value in one text field, once i finsih the second text field keyboard should open, and i need to enter there..ist like focus on next next text field..when i enter 1 value in all text field – doubtman Jul 28 '17 at 12:31
  • okey you mean to say second digit shoud be entered in second textField Right? – Jaydeep Vyas Jul 28 '17 at 12:32
  • yes, and only one digit have to enter in one text field....for say : if i am in first text field, and entered 1 digit and i should not enter 2 or more digits in first text field, then once i finish enter the first digit in first text field, the second text field keyboard should open to enter my second text field.. – doubtman Jul 28 '17 at 12:34
  • Okey @doubtman finaly completly Find solution for you wait i am posting answer – Jaydeep Vyas Jul 28 '17 at 12:56
  • now i need to connect that iboutlet to all my 6 text fields right ???... – doubtman Jul 29 '17 at 04:44
  • any solution for here https://stackoverflow.com/questions/45667948/how-to-add-followed-text-to-uitextfield – doubtman Aug 14 '17 at 10:25