have the following:
// watch the fields
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleTextChange:)
name:UITextFieldTextDidChangeNotification
object:textField1];
and then:
-(void) handleTextChange:(NSNotification *)notification {
...
}
Have a breakpoint in -handleTextChange:, but doesn't get fired.
textField is connected in the Interface Builder.
Works on iOS6 iPhone/iPad simulator, on iOS5.1 iPad2, but not on iOS6 iPad3.
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];
来源:https://stackoverflow.com/questions/12754948/uitextfieldtextdidchangenotification-doesnt-get-called-on-ios6-ipad3