Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute current UITextField autocorrect suggestion when another button is pressed

I am implementing chat in my application, very similar to the iPhone's built-in Messages app. I have a UITextField next to a button. The user types something into the text field, and very often the text field suggests various autocorrections. In the built-in Messages app, tapping the Send button will cause the currently visible autocorrection suggestion to execute. I am seeking this behavior in my application, but haven't been able to find anything.

Does anyone know of a way to programmatically execute the currently visible autocorrection/autocomplete suggestion of a UITextField when a completely separate control is activated? It's obviously possible somehow.

like image 488
Jeremy Fuller Avatar asked Jan 06 '11 03:01

Jeremy Fuller


3 Answers

Call -resignFirstResponder on the field. That forces it to accept the autocorrect. If you don't want to dismiss the keyboard, you can immediately follow that with a call to -becomeFirstResponder again.

like image 72
Lily Ballard Avatar answered Nov 10 '22 01:11

Lily Ballard


For esilver: you can do this without resigning first responder by having a different textfield becomeFirstResponder and then having the relevant textfield becomeFirstResponder. The keyboard will not move in this case, and not trigger any hide notifications. If you don't have any other textfields, create a dummy textfield and set it to hidden = YES.

-(void)tappedSendButton:(id)sender
{
    // This hack is in place to force auto-corrections to be applied
    // before the text is sent.
    [self.dummyTextField becomeFirstResponder];
    [self.toolbar.textView becomeFirstResponder];

    [self sendChatWithBody: [self.toolbar.textView.text copy]];
}
like image 25
Nick Locking Avatar answered Nov 10 '22 01:11

Nick Locking


Since resigning and re-assuming first responder may have side effects (lots of notifications, keyboard show/hide triggers, etc), I've been looking for an alternative, less brutal way. After quite some search, I found this is all you need to do to accept autocorrections in a UITextView (or UITextField fwiw):

[textView.inputDelegate selectionWillChange: textView];
[textView.inputDelegate selectionDidChange: textView];

Hope this helps ;)

like image 7
Max Seelemann Avatar answered Nov 10 '22 00:11

Max Seelemann