Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

e.g. 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; } 
like image 764
karim Avatar asked Aug 10 '11 12:08

karim


People also ask

How do you make a text field non editable in Swift?

Set the Boolean variable to true, which disables editing in the text field.

What is a text field on Iphone?

A control that displays an editable text interface. iOS 13.0+ iPadOS 13.0+ macOS 10.15+ Mac Catalyst 13.0+ tvOS 13.0+ watchOS 6.0+

What is UITextField in Swift?

An object that displays an editable text area in your interface.


2 Answers

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. 

In Objective-C:

[textField addTarget:self                action:@selector(textFieldDidChange:)      forControlEvents:UIControlEventEditingChanged]; 

In Swift:

textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged) 

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.

like image 114
Daniel G. Wilson Avatar answered Sep 23 '22 20:09

Daniel G. Wilson


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.

UITextField Change Event

like image 24
William T. Avatar answered Sep 21 '22 20:09

William T.