Why is textFieldDidEndEditing: not being called?

有些话、适合烂在心里 提交于 2019-12-01 15:16:22

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!