UITextFieldTextDidChangeNotification doesn't get called on ios6 ipad3

时光怂恿深爱的人放手 提交于 2019-12-04 02:49:00
Mr. Cairo

Irena is correct, UITextFieldTextDidChangeNotification does not fire when the text field is set programmatically. However I would just like to clarify that it has nothing to do with iOS6, it has to do with the iOS 6 SDK. If you compile with the iOS 5.1 SDK, the UITextFieldTextDidChangeNotification notification will fire whenever the text field is changed, programmatically or otherwise, even if run on an iOS 6 device.

so I figured it out. What changed in IOS6 SDK is that if you change the text of textfield programmatically, it doesn't send a notification. I have a custom keyboard on all of those views. when I tap on a key, it changes the text field text value by adding whatever I typed in. In ios 5 it would send a notification "textdidchange", but not in ios6.

My use case was somewhat special, I was creating HH:MM:SS duration UITextField with characters entered from the back, therefore trapping characters in - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string and then returning return (NO); to forbid auto-update of UITextField... pre-iOS6, it called the notification, post-iOS6, I'm simply calling [[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidChangeNotification object:self.textField]; just before the return statement.

On my ipad3 & iOS6.0 notification UITextFieldTextDidChangeNotification work fine. put

[[NSNotificationCenter defaultCenter]  addObserver:self
                                      selector:@selector(handleTextChange:)
                                          name:UITextFieldTextDidChangeNotification
                                        object:textField1];

in your viewDidLoad

As a temporary workaround until Apple fixes this, you can use the following code example:

//view is a UITextField
NSString *temp = ((UITextField*)view).text;
((UITextField*)view).text = @"";
[((UITextField*)view) insertText:[NSString stringWithFormat:@"%@%@", @"-", temp]];

That code will continue to fire the event.

This works too:

[((UITextField*)view) sendActionsForControlEvents:UIControlEventEditingChanged];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!