iOS: “Message sent to deallocated instance” when resign first responder on a UITextView when its auto correction pop-up is shown

StackOverflow https://stackoverflow.com/questions/8488932

Question

I have a custom toolbar with a "Done" button for the input accessary view of my text view. When this "Done" button is tapped I want to resign the text view from the first responder, so I call:

[textView resignFirstResponder];

This will throw an error:

Thread 1: Program received signal: "EXC_BAD_ACCESS".

when the "Done" button is tapped while the auto correction is shown (See image below). The error still even I call:

if ([textView isFirstResponder] && [textView canResignFirstResponder]) [textView resignFirstResponder];

It seems like the text view is the first responder and can be resigned but I cannot resign it. How can I solve this error? Thank you.

Edit 1: I still want to enable auto correction.

Edit 2: Please take a look at the capture image below.

auto correction is shown

Edit 3: After turning on Zombies in the scheme settings, the logged message is:

-[TIZephyrCandidate wordOriginFeedbackID]: message sent to deallocated instance 0x52bbc50

but I don't know what is the meaning of this message and what to do next.

Edit 4: The method to resign first responder will be called when the "Done" button is touched up inside the button is added target and action by the following line of code:

[doneButton addTarget:self action:@selector(resignAllFirstResponders) forControlEvents:UIControlEventTouchUpInside];

which the resignAllFirstResponders is:

- (void)resignAllFirstResponders
{
    ...

    if ([textView canResignFirstResponder] && [textView isFirstResponder]) 
        [textView resignFirstResponder];

    ...
}
Was it helpful?

Solution 2

Let me answer my own question. Anyway please note that I'm not sure this is a good enough solution but I just want to share my current progress and still waiting for better solution.

The concept is to find out whether subviews of the text view contains a view of UIAutocorrectInlinePrompt which is the auto correction pop-up that cause the error or not. Then call the method resignFirstResponder only when the set of subviews not contain UIAutocorrectInlinePrompt. My code are like this:

NSMutableString *subviewMutableString = [[NSMutableString alloc] init];
[subviewMutableString setString:@""];
for (UIView *subview in textView.subviews)
{
    [subviewMutableString appendFormat:@"%@", subview];
}
if ([subviewMutableString rangeOfString:@"UIAutocorrectInlinePrompt"].location == NSNotFound)
{
    [textView resignFirstResponder];
}

This will not allow to resign text view from first responder when the auto correction pop-up is shown.

OTHER TIPS

if you using :

- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText 

UITextViewDelegate and change directly text in the method like :

- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText
{
 aTextView.text = @"hi";

 return YES;
}

is caused crash.

  1. spellchecking view was appeared,
  2. resignFirstResponder of UITextView,
  3. change text directly textView:shouldChangeTextInRange:replacementText delegate,

app will be crashed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top