Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

ios - UIControlEventEditingChanged doesn't get fired when using setText of UITextfield

I am currently trying to track the states of a textfield.

I am using a custom datePicker which sets the text via -setText

[self.textField setText:[self.dateFormatter stringFromDate:self.date]];

Inside my textFieldDelegate I wrote the following code:

-(void)viewDidLoad {
  [super viewDidLoad];
  self.travelToOnTextField.delegate = self;
  [self.travelToOnTextField addTarget:self action:@selector(travelToOnTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

-(void)travelToOnTextFieldDidChange:(id)sender
{
   NSLog(@"Event Handler called");
}

If I change the text through my custom Datepicker the Method travelToOnDateTextFieldDidChange doesn't get called. But if I change the text using my Computer Keyboard it gets called for some reason.

Is this intentional?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...