2010-10-23 9 views

Respuesta

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

extraño, basado en UIControlEventDidEnd y UIControlEventDidBegin, es de suponer que se UIControlEventValueChanged ... ¿Qué hace exactamente UIControlEventValueChanged hacer entonces? – Jarrod

19

En realidad, no existe ningún caso el valor cambiado para UITextField, utilizo UIControlEventEditingChanged

+0

Pensé que lo publicaría aquí para que nuevas personas vean –

8

que resuelva el problema de cambiar el comportamiento de shouldChangeChractersInRange. Si devuelve NO, iOS no aplicará los cambios internamente, sino que tendrá la oportunidad de cambiarlo manualmente y realizar cualquier acción después de los cambios.

- (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

Tenga en cuenta que establecer la propiedad del texto moverá el cursor al final del texto, lo que podría afectar la edición – jarnoh

+0

Esto no funcionará si el usuario toca el retroceso. – Harris

1

para SWIFT esto es muy útil -

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