In my code, i have around 8 UITextField
s in a UITableView
, each one in a row. I also have a UIBarButtonItem
that when selected, calls the method hideKeyboard
.
When the user selects a textfield, and then scrolls the tableview so that the textfield is not visible anymore but the keyboard stays shown and then hit the uibarbuttonitem i get a EXC_BAD_ACCESS.
The text fields are placed in each row programatically, through cellForRowAtIndexPath
. hideKeyboard
is simply [self.view endEditing:YES];
.
So my guess is that, since endEditing
searches for the textField that is the first responder and calls resign on it but when it became the first responder and i scrolled the tableview in a way that it wasn't shown, that cell must have been reused and the textfield must have been deallocated.. but the endEditing method would still be calling it to resign… thats when the crash is probably occurring.
I thought about getting a reference of the last firstResponder but if it was deallocated i couldn't resign it anyways.. i can't also call becomeFirstResponder and then resign in a random textfield because i don't know which ones are visible at that time.
Does anybody have any idea whats the reason for this crash? How would i dismiss the keyboard correctly?