2

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

    ...
}
Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
Protocole
  • 1,733
  • 11
  • 28
  • 42
  • Turn on Zombies in the Xcode 4 scheme settings. Replicate the crash and inspect which released object being messaged is raising the exception. – Mark Adams Dec 16 '11 at 05:58
  • @MarkAdams I went to **Xcode -> Product -> Edit Scheme...** and checked the box in front of **Enable Zombie Objects**. Is this what you mean by "Turn on Zombies in the Xcode 4 scheme settings"? If so, please take a look log message in my edited question again. Thanks. – Protocole Dec 16 '11 at 06:32
  • Ok so we know that we tried to send the `wordOriginFeedbackID` message to an instance of `TIZephyrCandidate` that's already been released. Now we try to find out why resigning with the autocorrect popup visible is causing this. Where is this method called in relation to the text view? – Mark Adams Dec 16 '11 at 06:54
  • Hold the phone. Just noticed that thread 1 is crashing. Are you trying to resign first responder from a background thread? UI objects can only be accessed on the main thread. Might explain this madness. – Mark Adams Dec 16 '11 at 06:57
  • @MarkAdams Please take a look at my edited question again for the way I call the method. For the issue regarding the thread please suggest me how to check whether the method is called on background thread or main thread. Thanks. – Protocole Dec 16 '11 at 07:25
  • I imagine you would know if you were using a background thread. We need to find out what `TIZephyrCandidate` is and why it's being sent `wordOriginFeedbackID` in response to the text view giving up first responder status. Do you have the `UITextViewDelegate` method, `-textViewDidEndEditing:` implemented? – Mark Adams Dec 16 '11 at 07:49
  • No, I don't have `- (void)textFieldDidEndEditing:(UITextField *)textField` implemented but there are other text view delegates which are `- (void)textViewDidBeginEditing:(UITextView *)textView`, `- (void)textViewDidChange:(UITextView *)textView`, and `- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text`. – Protocole Dec 16 '11 at 08:26

2 Answers2

2

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.

Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
yuch
  • 21
  • 2
0

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.

Protocole
  • 1,733
  • 11
  • 28
  • 42