0

I'm working on a project which needs a special text field to edit byte values. My current solution is a dedicated readonly textfield and a "..." button to open a popover as shown in the image below:

The current solution, popover

Now I try to make my solution more user friendly. My goals are these:

  • If the text field gets the first responder status, the popover automatically opens.
  • The complete text is selected.
  • If the user leaves the text field with tab or selecting any field outside of the popover, the popover should automatically close.
  • If the user types any valid number and suffix the byte value is updated (e.g. "10 GB")

Currently I'm a little bit clueless. My questions are these:

  • Where is the best location to detect in in the subclass when the text field got first responder?
  • How can I detect when the field resigns being first responder?
  • Are there other, simpler solutions?
Flovdis
  • 2,945
  • 26
  • 49
  • A better design might be a NSValueTransformer that employs a NSByteCountFormatter and some other magic to massage the value. – Gerd K Mar 27 '14 at 19:08
  • @GerdK I already use NSByteCountFormatter to convert the byte values into text. But this works only from byte value to text but not back. The popover provides also a logarithmic slider which simplifies the input of high values. – Flovdis Mar 27 '14 at 19:10
  • That's why I was suggesting a NSValueTransformer. It could convert input like 10k to 10,000 etc. in it's -reverseTransformedValue: method. An then use your NSByteCountFormatter in the -transformedValue: method. That avoids the popover and the messy logic connected to it. – Gerd K Mar 27 '14 at 19:15

1 Answers1

2

I could implement everything using - (BOOL)becomeFirstResponder as a hook to display the popover and observing the first responder to automatically hide the popover:

- (void)viewDidMoveToWindow
{
    [super viewDidMoveToWindow];
    [self.window addObserver:self forKeyPath:NSStringFromSelector(@selector(firstResponder)) options:0 context:NULL];
}

As a start point, I published a working project with the classes on GitHub (MIT License):

Project on GitHub

Flovdis
  • 2,945
  • 26
  • 49
  • I voted your answer up but ... "NSStringFromSelector(@selector(firstResponder))" is very a roundabout way of saying @"firstResponder". – George Oct 02 '14 at 00:30
  • 1
    @George That's true, but if you make a typo in `@"firstResponder"` you won't notice, but if you make a typo in `NSStringFromSelector(@selector(firstResponder))` you will get a compiler error. – Flovdis Oct 03 '14 at 09:58
  • Fair enough. Early feedback is always better. – George Nov 03 '14 at 19:06