Working on a new app, I'm running into the same problem described here in reguards to deselecting text that is input into an nstextfield: http://www.cocoabuilder.com/archive/cocoa/195313-nstextfield-how-to-deselect-text.html
There are plenty of questions around SO about doing this with NSTextViews, but I haven't been able to find a working answer for NSTextFields.
In my project, I have a window with the text field in it with a controller class that is also the text field's delegate. I have an IBAction for the text field that is sent on enter which performs actions depending on the text in the field, along with:
(void)controlTextDidChange:(NSNotification *)note
and
(BOOL)control:(NSControl *)control
textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
which handle some custom autocompleting as in the answer to Suppressing the text completion dropdown for an NSTextField
My problem lies with when I hit enter to submit the inputted text, the string in the field is entirely selected, but I would like to be able to deselect the text and have the insertion point at the end, as described in the first link.
There are multiple places I could have it deselect the text, but actually doing the deselecting isn't working. I have tried obtaining the field editor as described in the first link, I have also tried using the controlTextDidEndEditing: method since I do actually get a field editor in the controlTextDidChange: method above:
- (void)controlTextDidEndEditing:(NSNotification *)note{
NSTextView *textView = [[note userInfo] objectForKey:@"NSFieldEditor"];
[textView setSelectedRange:NSMakeRange([[self.ParseField stringValue]length]-1, 0)
affinity:NSSelectionAffinityUpstream
stillSelecting:NO];
}
I also tried disabling and reenabling editing on the field, but that didn't work either.
Something as simple as being able to send a moveDown: message to the text field would work for me, as it's be the same as hitting the down arrow, but the text field doesn't recognize that selector. (I thought NSTextField inherited from NSResponder, so it would have worked, but I guess not?)
Thank you for any help
[yourTextField becomeFirstResponder];
(see stackoverflow.com/questions/764179/focus-a-nstextfield) – Sudo