UITextField, automatically move to next after 1 character

后端 未结 6 624

Scenario: I have 4 UITextFields that only accept 1 character. Easy.

Problem: After I enter the 1 character, I want the next TextField to become active automatically with

6条回答
  •  一整个雨季
    2021-01-31 12:07

    The following is for Swift 5 and handles textfields as an array instead of individual fields.

    import UIKit
    
    class MyViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet var digitFields: [UITextField]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        digitFields.forEach {
            configureDigitField($0)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        digitFields[0].becomeFirstResponder()
    }
    
    fileprivate func configureDigitField(_ digitField: UITextField) {
        digitField.delegate = (self as UITextFieldDelegate)
        digitField.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
    }
    
    // Move to next field in digit fields if the value is populated
    @objc fileprivate func textFieldDidChange(textField: UITextField) {
        if textField.text?.count == 1 {
            let remaining = digitFields.filter { $0.text?.count == 0 }
            if remaining.count > 0 {
                remaining[0].becomeFirstResponder()
            } else {
                digitFields.forEach { $0.resignFirstResponder() }
            }
        }
    }
    

    Results in:

    This is dependent on the textfields being grouped in an array. This can be achieved in interface builder by configuring the collection of fields in the Outlet configuration screen:

    which can be reached from the view controller properties on the last tab item

    Note that you need to manually add the

    @IBOutlet var digitFields: [UITextField]!

    to your view controller before you can add the text fields to it.

    Summary of Code Behaviour

    • The view controller needs to be a UITextFieldDelegate to allow it to receive textfield events.
    • In the viewDidLoad function, each of the text fields in the array are initialised in the configureDigitField method
    • In the viewWillAppear method the first field in the array is readied to handle input (i.e. the first entry will take place in it)
    • The configureDigitalField function sets this view controller to receive events from the textfield (each of them as it is called for each textfield)
    • It also sets up a selector to call the textFieldDidChange function on result of a textfield edit changed event
    • textFieldDidChange method checks if the length of the text in the field is 1, and if so
    • checks for the remaining text fields where there is no value entered
    • takes the first remaining text field and sets it up to receive the next input
    • if no remaining fields are empty, resigns its position as first responder, so any more keypresses will not occur in any of the digit fields

提交回复
热议问题