简体   繁体   中英

Auto switching between UITextFields with Swift

I have 4 UITextFields in one UIView like this:

在此处输入图片说明

each UITextField limited to 4 characters. i want implement a auto switching between UITextFields with count characters of each UITextField .

i use shouldChangeCharactersIn for limitation characters:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let maxLength = 4
        var newString = String()
        let currentString: NSString = textField.text! as NSString
        newString = currentString.replacingCharacters(in: range, with: string)

        return newString.length <= maxLength
    }

and this is my switching implement:

func textFieldDidChange(_ textField: UITextField){

    let text = textField.text

    if text?.utf16.count == 4 {
        switch textField {
        case firstPartTextField:
            secondPartTextField.becomeFirstResponder()
        case secondPartTextField:
            thirdPartTextField.becomeFirstResponder()
        case thirdPartTextField:
            fourthPartTextField.becomeFirstResponder()
        default:
            break
        }
    }
}

my problem is switch to previous UITextField in deleting texts, i can not detect backSpace event when UITextField is empty.

Add target for textField in viewDidLoad()

firstTxt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    secTxt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    thirdTxt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    fourTXt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)

Than create selector method

func textFieldDidChange(textField: UITextField){

    let text = textField.text

    if text?.utf16.count == 1{
        switch textField{
        case firstTxt:
            secTxt.becomeFirstResponder()
        case secTxt:
            thirdTxt.becomeFirstResponder()
        case thirdTxt:
            fourTXt.becomeFirstResponder()
        case fourTXt:
            fourTXt.resignFirstResponder()
        default:
            break
        }
    }else{

    }
}

Make an outlet collection of all your textfields and configure them like so:

OutletCollection:
 @IBOutlet var textfields: [UITextField]! 
Add this in viewDidLoad:
func textFieldDidBeginEditing(_ textField: UITextField) {
        textField.text = ""
}

In the target function, you'll restrict the count in this manner:

 func textFieldDidChange(textField: UITextField) { let text = textField.text! if text.utf16.count >= 4 { if textField.tag < 4 { codeTextfields[textField.tag + 1].becomeFirstResponder() } else { textField.resignFirstResponder() } } } 

This will the change in one of your delegate methods:

 func textFieldDidBeginEditing(_ textField: UITextField) { textField.text = "" } 

Refined version of the following answer: How to move cursor from one text field to another automatically in swift ios programmatically?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM