0

Does anybody have a workaround for using a NSStepper control (many, in fact..) with an Increment set to 0.1 or anything other than integer values w/o calling setIncrement: on each and every NSStepper instance?

I've got an editor-like UI that requires steppers to change the values of their attaches NSTextField instanced by tiny increments.

Setting the increment to a decimal value in Interface Builder (IB) causes it to round it to an integer - although the API accepts a double.

Even entering nothing in the increment field and using a NSStepper subclass that calls setIncrement: for all instances setting it to e.g. 0.1 on init fails as IB apparently still sets the increment to 0 or something after init was called..

Not sure how it does that, though, as the setIncrement: override in the subclass isn't called...

Jay
  • 6,572
  • 3
  • 37
  • 65
  • How are the `NSStepper` instances created? Interface Builder or in code? – Monolo Feb 02 '13 at 16:45
  • There wouldn't be a problem if I create them in code, right? I'd set the increment right there. As mentioned in the post the values are set in IB, i.e. the `NSStepper`s are created in IB (Interface Builder) – Jay Feb 03 '13 at 07:34
  • No, that shouldn't be a problem - but then you'll also have to add them to your UI programmatically, so either way you'll have to do some work on the UI from your code. – Monolo Feb 03 '13 at 09:22
  • Hmm, afraid so. I was really hoping for a more elegant solution.. pity is, this is a common problem for potentially dozens of views with text edit/stepper combos (think IB like UI for properties/dimensions). – Jay Feb 04 '13 at 08:18
  • You're setting the Step value for your stepper to 0.1 in IB, yet when you try to use it the value gets rounded to an integer. Is that right? – beev Feb 04 '13 at 09:49
  • @beev - IB rounds decimals entered for the `NSStepper` increment to integer values, that's right. – Jay Feb 05 '13 at 15:47
  • Not for me it doesn't. I have mine stepping at 0.5, set in IB. It works fine. Are you setting it under "Step" in IB? – beev Feb 05 '13 at 16:55
  • @beev - there's no 'Step' for me. This is Xcode 4.2 under 10.6, apparently we're running different versions? – Jay Feb 06 '13 at 06:21
  • Yeah, mine is 4.6. Are you able to upgrade? It's best to stay up to date if possible. – beev Feb 06 '13 at 20:47
  • @beev - Gotta stay on 10.6.x for now due to project constraints :-( – Jay Feb 07 '13 at 08:37

1 Answers1

1

If you have a lot of NSSteppers which differ from the standard one only by the increment, then you could try to subclass NSStepper and override initWithCoder: which is the initialiser called when an object is re-created from a nib file.

I tend to consider initWithCoder: a designated initialiser together with initWithFrame:, but the docs are little vague on that aspect. Anyway, if you call super before you set your own properties, it should work and be robust. If you do this sort of trick you could also consider overriding initWithFrame: to do the same initialization, so you get consistent behaviour across the different ways to instantiate the subclass.

The downside to this approach is that you will not be able to set the increment in Interface Builder for your custom subclass, so you might want to document it well for posteriority.

Monolo
  • 18,205
  • 17
  • 69
  • 103