UIControlEventEditingChanged doesn't get fired when using setText of UITextfield
I had to rate this question up because this is super important.
It appears this has changed in iOS6. Maybe even iOS5. This event USED to be the preferred method for observing text changes in a UITextField. I've been using it since iOS3. After recompiling my app on iOS6 to perform some updates, it mysteriously stopped working. Parts of my app use:
[UITextField insertText];
and others use
[UITextField setText];
or
UITextField.text = @"xxx";
For whatever reason it's important to note that setText events no longer fire the Editing Changed event. If you use an insertText method, it still works.
So if you're in a bind and you just need a quick fix, you can change out your code from:
textField.text;
or
[textField setText:@"xxx"];
to
textField.text = @"";
[textField insertText:@"new text"];
This also works:
[((UITextField*)view) sendActionsForControlEvents:UIControlEventEditingChanged];
I hope this helps someone, because I wasted an entire day trying to figure out why this code suddenly stopped working when I moved up to iOS6.
You can still use NSNotificationCenter as well. See the UITextField reference for more info:
https://developer.apple.com/documentation/uikit/uitextfield
UITextField value changed not firing when field is updated
As suggested by TheRonin, use UITextFieldDelegate
and implement the following method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
UITextField with secure entry, always getting cleared before editing
Set,
textField.clearsOnBeginEditing = NO;
Note: This won't work if secureTextEntry = YES. It seems, by default, iOS clears the text of secure entry text fields before editing, no matter clearsOnBeginEditing is YES or NO.
Trigger textFieldShouldClear programmatically
I ended up subclassing UITextField
and implementing the following method:
- (void)setText:(NSString *)text {
[super setText:text];
[self textInputChanged:nil];
}
Where textInputChanged:
is the UITextFieldDelegate
method that I needed to get called whenever text is set programmatically.
Changing value of UITextField using an array doesn't work
Setting the array:
NSArray *lstaInfo = @[@"fruits",@"bee", @"computer", @"car"];
Then populating fields:
-(void)setFields{
field0.text = lstaInfo[0];
field1.text = lstaInfo[1];
field2.text = lstaInfo[2];
field3.text = lstaInfo[3];
}
UITextField text change event
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
.
Related Topics
Detect Moment When Newline Starts in Uitextview
"The Run Destination iOS Device Is Not Valid for Running the Scheme"
How to Dynamically Format a Number to Have Commas in a Uitextfield Entry
Error in iOS 10:Unable to Copy Asset Information from Https://Mesu.Apple.Com/Assets/ for Asset Type
How to Use Uiviewcontrolleranimatedtransitioning with Uinavigationcontroller
Swiftui Buttonstyle - How to Check If Button Is Disabled or Enabled
What Is the Impact of the "Requires Full Screen" Option in Xcode for an Iphone-Only App
Linker Error When Accessing Application Module in UI Tests in Xcode 7.1
How Should a Swift + Objective-C Project Be Setup for Unit Testing
How to Draw a Route Between Two Locations Using Mapkit in Swift
How to Get Audio Controls on Lock Screen/Control Center from Avaudioplayer in Swift
How to Properly Animate Uiscrollview Contentoffset
Alamofire No Such Module (Cocoapods)
iOS Swift Converting Calendar Component Int Month to Medium Style String Month