-5

There is a scenario when user enters two character in textfield then focus should move to next textfield.

Screenshot

Below is code that I have used :

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        if let text = textField.text {

            let newStr = (text as NSString)
                .stringByReplacingCharactersInRange(range, withString: string)
            if newStr.isEmpty {
                return true
            }
            let intvalue = Int(newStr)

            if textField.tag == 101 { print("101") // only 2 integer
                 return (intvalue >= 0 && intvalue <= 99) ? true : false
            }
            else if textField.tag == 102 { print("102") // only 4 integer
                 return (intvalue >= 0 && intvalue <= 9999) ? true : false
            }

        }
        return true
    }

How can this be acheived?

Also, numeric keypad dosen't have Done button?

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177

3 Answers3

5

You should tell the next textfield to become the first responder after the user enters 2 characters like so:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if (textField.text?.characters.count == 2) {
        textField.resignFirstResponder()
        yourNextTextField.becomeFirstResponder()
    }
    return true
}
LuKenneth
  • 2,197
  • 2
  • 18
  • 41
  • I cannot use code in shouldChangeCharactersInRange since it is already occupied with other validation stuff. Another approach found is to use Observer. – Jayprakash Dubey Jul 25 '16 at 12:52
  • You should still be able to use shouldChangeCharactersInRange, you can just check which textfield is being changed before reassigning the first responder – LuKenneth Jul 25 '16 at 12:54
  • See the newly posted answer. That is what I was expecting. Thanks for your post! – Jayprakash Dubey Jul 25 '16 at 12:57
1

A possibility for a Next and a Done button would be to use an UIToolbar above the numeric keypad. This way, you would assign a function for those two buttons, as they are UIBarButtonItem.

I have some sample codes in Objective C, if you need me to, I can convert it to Swift.

To create the UIToolbar, use this :

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
toolBar.barStyle = UIBarStyleBlackTranslucent;

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nextIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(nextTouched:)];

// the middle button is to make the Done button align to right
[toolBar setItems:[NSArray arrayWithObjects:nextButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
picker1.inputAccessoryView = toolBar;
picker2.inputAccessoryView = toolBar;
picker3.inputAccessoryView = toolBar;

And to implement Next feature, use this kind of code :

-(void) nextTouched:(UIBarButtonItem*)itemClicked {
    if [([picker1 isFirstResponder])
        [picker2 becomeFirstResponder];
    else if [([picker2 isFirstResponder])
        picker3 becomeFirstResponder];
}
AnthoPak
  • 4,191
  • 3
  • 23
  • 41
-1
// This code in ViewDidLoad
txtCardDetails3.addTarget(self, action: #selector(VerifyCardViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)

func textFieldDidChange(textField: UITextField){

        let text = textField.text

        if textField.tag == 101 { // Set tag at design time
            if text?.utf16.count==2 {
                txtCardDetails4.becomeFirstResponder()
            }
        }
    }

// This code as it is 
 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        if let text = textField.text {

            let newStr = (text as NSString)
                .stringByReplacingCharactersInRange(range, withString: string)
            if newStr.isEmpty {
                return true
            }
            let intvalue = Int(newStr)

            if textField.tag == 101 { print("101")
                 return (intvalue >= 0 && intvalue <= 99) ? true : false
            }
            else if textField.tag == 102 { print("102")
                 return (intvalue >= 0 && intvalue <= 9999) ? true : false
            }

        }
        return true
    }
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177