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
In a simple app with just a window and textfield.
I set the textField to firstResponder.
This makes the text selected when the app runs.
If I add to the the applicationDidFinishLaunching
NSRange tRange = [[ _theTextField currentEditor] selectedRange];
[[ _theTextField currentEditor] setSelectedRange:NSMakeRange(tRange.length,0)];
Now when run the Text is not selected and the insertion point is at the end of the text.
I used selectedRange to get the selection char length. As the text was selected in the first place and seem more simple.
- (void)awakeFromNib{
[_theTextField setStringValue:@"100000"];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[[[NSApplication sharedApplication] mainWindow] makeFirstResponder:_theTextField];
NSRange tRange = [[ _theTextField currentEditor] selectedRange];
[[ _theTextField currentEditor] setSelectedRange:NSMakeRange(tRange.length,0)];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With