2017-04-18 36 views

回答

3

根據您的要求,我做了一個演示,它的功能就像一個魅力。 因此,爲了滿足您的要求,您必須遵循以下說明:

給標籤的每一個的UITextField爲了像101,102,103,104,105和106

然後,讓每一個文本框的委託,以自己(的UIViewController)

然後實現UITextField的以下代理方法

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
     print("string = \(string) text = \(textField.text)") 

     if(textField.text?.characters.count == 1){ 

      if string.characters.count == 0{ 


       if textField.tag != 101{ 
        textField.resignFirstResponder() 

        let preField : UITextField = self.view.viewWithTag(textField.tag-1) as! UITextField 
        preField.becomeFirstResponder() 
       } 

       textField.text = string 
       return false 
      } 


      if textField.tag != 106{ 
       textField.resignFirstResponder() 

       let nextField : UITextField = self.view.viewWithTag(textField.tag+1) as! UITextField 

       nextField.text = string 
       nextField.becomeFirstResponder() 
      } 

      return false 
     } 

     return true 
    } 
+0

它工作完美,謝謝 –