UITextField text change event
How can I detect any text changes in a textField? The delegate method shouldChangeCharactersInRange
works for something, but it did not fulfill my need exactly. Since until it returns YES, the textField texts are not available to other observer methods.
eg in my code calculateAndUpdateTextFields
did not get the updated text, the user has typed.
Is their any way to get something like textChanged
Java event handler.
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
if (textField.tag == kTextFieldTagSubtotal
|| textField.tag == kTextFieldTagSubtotalDecimal
|| textField.tag == kTextFieldTagShipping
|| textField.tag == kTextFieldTagShippingDecimal)
{
[self calculateAndUpdateTextFields];
}
return YES;
}
From proper way to do uitextfield text change call back:
I catch the characters sent to a UITextField control something like this:
// Add a "textFieldDidChange" notification method to the text field control.
[textField addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
Then in the textFieldDidChange: method you can examine the contents of the textField, and reload your table view as needed.
You could use that and put calculateAndUpdateTextFields as your selector
.
XenElement's answer is spot on.
The above can be done in interface builder too by right-clicking on the UITextField and dragging the "Editing Changed" send event to your subclass unit.
to set the event listener:
[self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
to actually listen:
- (void)textFieldDidChange:(UITextField *)textField {
NSLog(@"text changed: %@", textField.text);
}
链接地址: http://www.djcxy.com/p/28170.html
上一篇: 在从NSMutableDictionary加载的textview中保存已更改的文本
下一篇: UITextField文本更改事件