ResignFirstResponder calling EXC_BAD_ACCESS

I have a UITextField on a UITableViewCell and a button on another cell.

I click on UITextField (keyboard appears).

UITextField has the following method:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { NSLog(@"yes, it being called"); owner.activeTextField = textField; return YES; }; 

If owner.activeTextField is a (conservation, non-atomic) property.

Problem When the keyboard is visible, I am viewing a cell from the view. Then I press the button located on another cell. The button calls:

 [owner.activeTextField resignFirstResponder] 

And that calls EXC_BAD_ACCESS.

Any idea? The cell is definitely in memory. My assumption is that as soon as it disappears, it is removed from the view, and one of its properties (parent view?) Becomes zero and causes the indicated error.

I'm right?

TL DR; How to remove the keyboard (cancel the first responder) when the UITextField is removed from the view?

+4
source share
3 answers

Sometimes the problem can be on a different level ... Check and make sure that the next object in the responder chain (the one that subsequently receives the startFirstponder message) is not garbage. Just a thought.

+2
source

Have you checked owner.activeTextField , to see if it has been canceled / set to nil? Not sure if this will call EXC_BAD_ACCESS , but worth a try.

Also do you have any calls to NSNotificationCenter ? I struggled with something similar today that caused EXC_BAD_ACCESS on becomeFirstResponder , which was caused by what I called [[NSNotificationCenter defaultCenter] removeObserver:keyboardObserver]; on incorrect deletion.

0
source

A bit out of date, but since I had the same problem with an outdated application for manually counting links, I will try. Note. This problem should not occur with ARC anymore (and if so, my solution certainly does not fit there ...).

It seems that:

  • the text box is saved in the cell and saved (possibly above)
  • when you scroll a cell using a text box, the cell gets recycled (which is good), but the text box is not (so the text box is still in memory on error)
  • at this moment, rejecting the keyboard, the first responder for the text field is correctly canceled, but as the call moves down the presentation hierarchy, it falls into a cell that is no longer in memory.

a simple (and imo elegant) solution to the problem would be

  • commit over hold uitextfield if any
  • subclass of UITextField to discard the status of the first responder before release

one method, for example:

 - (void) dealloc { [self resignFirstResponder]; [super dealloc]; } 

which will have a side effect when you remove the keyboard as soon as the cell goes out of view.

Another solution (which I chose for various reasons) would be to manually save and recycle the cell with the text box until the table is freed.

I am sure that you have already solved your problem, but I hope this helps someone else ...

0
source

All Articles