2010-10-23 5 views

Répondre

120
[myTextField addTarget:self 
       action:@selector(textFieldDidChange:) 
     forControlEvents:UIControlEventEditingChanged]; 
+18

Bizarre, basé sur UIControlEventDidEnd & UIControlEventDidBegin, on peut supposer qu'il a été UIControlEventValueChanged ... Que signifie exactement UIControlEventValueChanged alors? – Jarrod

19

En fait, il n'y a pas de valeur événement changé pour UITextField, utilisez UIControlEventEditingChanged

+0

Je pensais que je l'afficherais ici pour que les nouvelles personnes voient –

8

Je résolu le problème de changer le comportement des shouldChangeChractersInRange. Si vous renvoyez NO, les modifications ne seront pas appliquées par iOS en interne, mais vous aurez la possibilité de les modifier manuellement et d'effectuer toute action après les modifications.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
    //Replace the string manually in the textbox 
    textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; 
    //perform any logic here now that you are sure the textbox text has changed 
    [self didChangeTextInTextField:textField]; 
    return NO; //this make iOS not to perform any action 
} 
+2

Notez que la définition de la propriété text déplace le curseur à la fin du texte, ce qui peut affecter l'édition – jarnoh

+0

Cela ne fonctionnera pas bien si l'utilisateur recule en arrière. – Harris

1

Pour ce rapide est très pratique -

textField.addTarget(self, action: #selector(onTextChange), forControlEvents: UIControlEvents.EditingChanged)