I've got a UITextField, and when text gets entered into that, I want to obtain the doubleValue from the text field and perform some computations and display them in a UITableView.
The delegate for the UITextField adopts the UITextFieldDelegate protocol, and implements both textFieldShouldReturn: and textFieldDidEndEditing: methods. textFieldShouldReturn: resigns first responder status which, according to docs should also trigger textFieldDidEndEditing:, but I never see textFieldDidEndEditing: called.
- (BOOL)textFieldShouldReturn:(UITextField*)theTextField {
if (theTextField == thresholdValue) {
[thresholdValue resignFirstResponder];
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self updateThresholdValue:textField];
}
It may be worth noting that I also tried connecting some of the textfield events to the delegate and having the event call updateThresholdValue: directly, but that didn't work either.
Just ran into the same problem you described. After trying everything I could think of I added this delegate method:
// if we encounter a newline character return
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// enter closes the keyboard
if ([string isEqualToString:@"\n"])
{
[textField resignFirstResponder];
return NO;
}
return YES;
}
Now the textFieldShouldEndEditing fires and the text field resigns first responder.
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
textFieldDidEndEditing is fired when the textfield resigns it's first responder status while textFieldShouldReturn is fired when the return button is pressed.
It sounds like your textfield is never resigning as firstResponder. You can check it pretty easily by putting some debug output (as suggested in the comments) and just navigating out of the textfield with a touch - eg start typing then just touch outside of the field to force it to resign firstResponder.
Not sure if that helps a lot, but it sounds like a strange case you are hitting.
As mentioned in one of the comments, I tried replicating a simpler version of what I was trying to do using a HelloWorld app, and it worked on the first try, with no problems, just as expected. Faced with that, I began to wonder a bit.
Somewhere in all the googling I did to try to get an answer to this problem, I ran across a link where a person was having a problem with events not firing from a button, and it turned out that the problem was related to the view in which the button was placed and that view having another subview as a peer to the button, and that subview was somehow eating the events rather than allowing them to go where they'd been directed. The scenario wasn't exactly the same as mine, but it sounded close enough that investigation would be worthwhile. Despite all the excellent advice received here, none had yet borne fruit.
Last night I decided to completely re-create the nib file from scratch (made a copy of what I had first), and I got it to work. The key difference seems to be that I in the original non-working nib file, I had dragged over a TableViewController
from the palette, then changed its type to my subclass PZTableViewController
. When I did that same thing in my new nib, the events didn't fire. If, on the other hand, I removed the dragged-over TableViewController
and instead just dragged over an NSObject
and changed its class to PZTableViewController
and plumbed everything up, it all "just worked".
I will try to chase down the link that got me started on this line of thinking and post it either as an edit or as a comment.
This is also not a complete answer because I do not yet understand the difference between the from-the-palette TableViewController
and my subclass. When I figure that out, I will update this answer via edit or comment.
I ended up wiring up editingChanged to record every single change to a text field rather than figuring out how to cover all the corner cases with the remaining callbacks.
Very "efficient" due to the android-like "usability" of the textFieldDidEndEditing (sarcasm).
If you adopted UITextFieldDelegate protocol in your view controller, then write the following line of code in viewDidLoad method to activate the methods of above protocol to your corresponding textFields:
override func viewDidLoad() {
//other stuff
yourTextField.delegate = self
}
来源:https://stackoverflow.com/questions/1228298/why-is-textfielddidendediting-not-being-called