Search code examples
iosuitextviewdeallocresignfirstresponderautocorrect

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


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];

    ...
}

Solution

  • 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.