2017-08-25 2 views
0

J'ai plusieurs restrictions que j'aimerais ajouter à 2 champs de texte séparés.Arguments de champ de texte multiples Swift

TextField1 , TextField2

Les deux champs de texte

  • Autoriser uniquement une décimale

  • Autoriser seulement les 8 caractères de longueur

« shou ldChangeCharactersIn "- Comment puis-je utiliser cette fonction pour appliquer à plusieurs textFields et avoir plusieurs contraintes?

func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) -> Bool 
    { 
    let countdots = (capitalInvested.text?.components(separatedBy: ".").count)! - 1 

     if countdots > 0 && string == "." 
     { 
      return false 
     } 
     return true 
     } 
+0

contrôle dans '' si shouldChangeCharactersIn' (textField == TextField1 || TextField2) {// applique vos deux conditions} ' –

Répondre

4

Vous devez vérifier quel champ texte est tapé, qui appelle la fonction:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    if textField == textField1 { 
     // first text field 
     // add restrictions here 
    } else if textField == textField2 { 
     // second text field 
     // add restrictions here 
    } 
    if textField == textField1 || textField == textField2 { 
     // restrictions for both 
    } 
} 
+0

réponse correcte .. – Mehul