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

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

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.
spellchecking view was appeared,
resignFirstResponder of UITextView,
change text directly textView:shouldChangeTextInRange:replacementText delegate,
app will be crashed.

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.

Related

Detect when a user presses the space button

I currently have my keyboard set so the type is Numbers and Punctuation [so users can type in a hypen symbol]. Does anyone know how to detect if the user presses the space bar so I can automatically set the style to a different keyboard type [in my case letters] so I don't have to build a custom keyboard?
Try this code - I am sure that it will detect if a space character is coded in - I have not really tested how well the keyboard switch works. You should also try it out without the resignfirstresponder becomefirstresponder code once the code below is working. Good Luck! (PS your viewCOntroller needs to be declared as a UITextview or textfield Delegate)
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString *)text_message {
if ([text_message isEqual:#" "]) {
// Put your code to change the keyboard then refresh the screen.
[textView resignFirstResponder]; //close textview or textfield
[textView setKeyboardType:UIKeyboardTypeDefault];
[textView becomeFirstResponder]; // opens keyboard for textfield or textview
[self.view setNeedsRefresh]; // refresh view
// return NO;
}
return YES; //go back to editing
}

Click spacebar on the keyboard programmatically

I have an issue with a resizing UITextView
It resizes correctly when user is typing and resizes incorrectly, when i set its text programmatically
with [textView setText:]
I want to set its text to blank by doing setText:#"" and then clicking the spacebar programmatically
How do i click the space bar programmatically ?
Here are screenshots of my problem
More than likely you've put code in (void)textViewDidChange:(UITextView *)textView that doesn't belong there.
The text view calls this method in response to user-initiated changes
to the text. This method is not called in response to programmatically
initiated changes.
The solution is to create a separate method that runs regardless of whether the text was changed programmatically or by the user. It will probably looks something like this.
- (void)setText:(NSString *)text {
self.myTextView.text = text;
[self updateTextViewSize];
}
- (void)textViewDidChange:(UITextView *)textView {
[self updateTextViewSize];
}
- (void)updateTextViewSize {
//sizing logic goes here
}

Using next and previous button with UITextField, problems with firstresponder

I am using a subclass of a scrollview that moves the keyboard out of the way. (TPKeyboardAvoidingScrollView)
I think this is conflicting with my implementation of the next and previous buttons. I have built an inputaccessoryview
I have a category that sets the next and previous textfields for each field
when i edit a textfield, i set the current, previous and next textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
_currentTextField = textField;
_prevTextField = [textField prevTextField];
_nextTextField = [textField nextTextField];
return YES;
}
when they click next or previous i call this method
- (void)selectAdjacentResponder:(id)sender{
UISegmentedControl *segmented = sender;
if(segmented.selectedSegmentIndex == 0){
[_prevTextField becomeFirstResponder];
} else {
[_nextTextField becomeFirstResponder];
}
}
this works fine.. but when i close the keyboard. my scrollview is messed up. if i add the line
[_currentTextField resignFirstResponder];
to the first line of my selectadjacent method it solves the problem. but the problem is it makes the screen focus in a funky way since i'm dismissing and accessing the next textfield at the same time.
i have tried resigning first responder when i close the keyboard. but i think my scrollview is calculated before that point.. any idea what to do =/

Remove keyboard when table cell selected

I have a modal set up like this:
Despite appearances, the first two fields are UITextFields that become first responders and display the keyboard. The third 'Department' item is a table view and pushes another view.
I have implemented a scroll view so that while either of the fields is being edited, the user can scroll around and reach the department cell:
When it is selected, before the push, I would like to hide the keyboard. It's a small detail, but try adding a new event on Apple's 'Calendar' app. It opens with the first text field as first responder (so keyboard is present). If you select the start/end cell, the keyboard hides as the next view is pushed.
How do I achieve this? As a test I tried:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"hiding keyboard");
if ([ingredientName isFirstResponder]) {
NSLog(#"resigning name field");
[ingredientName resignFirstResponder];
}
else if ([ingredientAmount isFirstResponder]){
NSLog(#"resigning amount field");
[ingredientAmount resignFirstResponder];
}
}
The logging confirms that these are getting called. But the keyboard does not hide. It simply stays in place as the view slides out. And on return, the previous field still has focus (and the keyboard is out).
Any ideas?
resignFrstResponder will not dismiss the keyboard when you're using a modal view.
Another dev found a workaround for this behavior here: http://viraj-workstuff.blogspot.com/2010/12/resignfirstresponder-does-not-hide.html
Not terribly elegant, but try stealing the firstresponder status with an ad-hoc, invisible UITextField:
-(void)dismissKeyboard {
UITextField *textField;
textField=[[UITextField alloc] initWithFrame:CGRectZero];
[self.view addSubview:textField];
[textField becomeFirstResponder];
[textField resignFirstResponder];
[textField removeFromSuperview];
// [textField release] // uncomment if not using ARC
}
Two things:
Are you using a segue for pushing that other VC? If so, resign the keyboard in the prepareForSegue method.
If not, try using willSelectRowForIndexPath instead of didSelectRowAtIndexPath

Force keyboard to show up via button (iOS)

I have an UITextView and I don't want check editable option, how can call keyboard via a button?
This code doesn't work for me!
-(IBAction) yourButtonClick
{
[myTextView becomeFirstResponder];
[self.view addSubview:myTextView];
}
From the iPhone Application Programming Guide
However, you can programmatically
display the keyboard for an editable
text view by calling that view’s
becomeFirstResponder method. Calling
this method makes the target view the
first responder and begins the editing
process just as if the user had tapped
on the view.
So to show the keyboard programmatically,
[textView becomeFirstResponder];
However, the keyboard will never show if the textView is not editable.
The purpose of showing the keyboard is to allow editing. I assume you just don't want the keyboard to appear when the user taps the text view. In this case, you can enable editable programmatically when the button is tapped.
-(IBAction) yourButtonClick
{
myText.editable = YES;
[myText becomeFirstResponder];
}
Then in the UITextViewDelegate, disable editable when the user finishes editing.
- (void)textViewDidEndEditing:(UITextView *)textView {
textView.editable = NO;
}

Resources