title: Auto switching between UITextFields with Swift
body: <p>I have 4 <code>UITextFields</code> in one <code>UIView</code> like this:</p>
  
  <p><a href="https://i.stack.imgur.com/HYbgL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HYbgL.png" alt="enter image description here"></a></p>
  
  <p>each <code>UITextField</code> limited to 4 characters. i want implement a auto switching between <code>UITextFields</code> with count characters of each <code>UITextField</code>.</p>
  
  <p>i use <code>shouldChangeCharactersIn</code> for limitation characters:</p>
  
  <pre><code>func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -&gt; Bool {
          let maxLength = 4
          var newString = String()
          let currentString: NSString = textField.text! as NSString
          newString = currentString.replacingCharacters(in: range, with: string)
  
          return newString.length &lt;= maxLength
      }
  </code></pre>
  
  <p>and this is my switching implement:</p>
  
  <pre><code>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
          }
      }
  }
  </code></pre>
  
  <p>my problem is switch to previous <code>UITextField</code> in deleting texts, i can not detect backSpace event when <code>UITextField</code> is empty.</p>