2010-10-23 21 views

risposta

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

Strano, sulla base di UIControlEventDidEnd & UIControlEventDidBegin, si potrebbe supporre che è stato UIControlEventValueChanged ... Che cosa fa esattamente UIControlEventValueChanged fare allora? – Jarrod

19

In realtà, non v'è alcun valore dell'evento cambiato per UITextField, uso UIControlEventEditingChanged

+0

Ho pensato di postarlo qui per le nuove persone per vedere –

8

Ho risolto il problema cambiando il comportamento dei shouldChangeChractersInRange. Se si restituisce NO, le modifiche non verranno applicate internamente da iOS, ma si avrà la possibilità di modificarlo manualmente ed eseguire qualsiasi azione dopo le modifiche.

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

Nota che l'impostazione della proprietà del testo sposterà il cursore alla fine del testo, che potrebbe influire sulla modifica – jarnoh

+0

Questo non funzionerà bene se l'utente colpisce backspace. – Harris

1

per SWIFT questa viene portata di mano -

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