0

I've been trying to figure out how to auto erase a string within an NSTextField upon clicking within in it.

For example the current behavior is like this on OSX:

nstextfield1 nstextfield2

When a user clicks in the cell or the default focus is the cell either the word is sometimes highlighted or the cursor is placed in the middle of the text. The user then needs to use backspace to delete the text (which I'd like to eliminate by automatically removing the text).

Is this a function that is built-in that I'm somehow missing? If not how would I go about it?

Tim
  • 8,932
  • 4
  • 43
  • 64
Joe Habadas
  • 628
  • 8
  • 21
  • Don't do this, the user might not know about this behavior and lose their work. – SevenBits Feb 15 '15 at 19:29
  • @SevenBits, It's not their work, it's mine, and it's one textfield. I use a prefilled string in it as a placeholder, but well, as a string. – Joe Habadas Feb 15 '15 at 19:30
  • The `NSTextField` _has_ an actual placeholder string attribute, so if that's what you're doing, I'd suggest using the existing functionality. – jscs Feb 15 '15 at 19:49
  • @JoshCaswell, The placeholder won't disappear (auto erase) when clicking within the cell either, it only does so when starting to type. I'd like to perform this on a string since it's able to be highlighted when in focus. – Joe Habadas Feb 15 '15 at 20:08
  • You want to make your app work differently from all other apps? Go ahead. – gnasher729 Feb 15 '15 at 20:16
  • So you want to clear the contents _either_ when the field gets focus _or_ when it has focus and the text is highlighted? (There is nothing built in that does what you want because as SevenBits noted, it's kind of user-hostile. But it shouldn't be difficult to write.) – jscs Feb 15 '15 at 20:21
  • @gnasher729, It's safe to assume you aren't one that thinks outside the box. – Joe Habadas Feb 15 '15 at 20:23
  • @JoshCaswell, I'm thinking when the users clicks within the field or used tab from the previous one it would be nice to have the text automatically disappear (as if they hit backspace and deleted the existing string). I basically have prefilled values in fields and the presentation just works better when a user clicks one of them and the text instantly disappears. I'm not sure how to go about implementing the functionality though, thanks. – Joe Habadas Feb 15 '15 at 20:31
  • @JoshCaswell, It might work nicely also if the user didn't type anything after clicking and then changing focus that the string went back to whatever was there before. That might solve the possible hostility that's been mentioned. – Joe Habadas Feb 15 '15 at 20:35
  • @JoshCaswell, Here's a perfect example - http://jsfiddle.net/29mrojf0/ – Joe Habadas Feb 15 '15 at 20:51

2 Answers2

0

As far as I know there is no standard function to delete the text that is in the textfield. Depending on your controller class you can try to work with mouseDown or acceptFirstResponder, but I don't believe this will provide a stable situation.

I would recommend you to start with an empty textfield (setting in xcode) and empty the textfield after the content of the textfield has been processed. For instance:

-(IBAction)addYourTextFieldInWhateverYouWant:(id)sender
{
    //copying the string from the textfield
    NSString *text = [[self yourTextField]stringValue];

    //now you can empty the textfield
    [[self yourTextField]setStringValue:@""] 

    //Do here whatever you need to do with the input from the textfield.
    ....
}

It doesn't give you exactly what you want, but it provides the user with an empty textfield where the user can immediately type in his/her text when the textfield is clicked upon and the user doesn't need to delete any text in the textfield.

Hope this helps.

Kind regards, MacUserT

MacUserT
  • 1,760
  • 2
  • 18
  • 30
0

There are plenty of terrible ideas “outside the box”, and this is one of them.

Anyway, make a subclass of NSTextField. Override becomeFirstResponder. If [super becomeFirstResponder] returns YES, set self.stringValue = @"".

rob mayoff
  • 375,296
  • 67
  • 796
  • 848