views:

257

answers:

1

What I'm trying to accomplish is two fold:

  1. I want my NSTextField to act like a "microwave timer" and update as characters are entered. So the field would default to "00:00" (minutes, seconds). When a 1 is pressed, the field would look like "00:01". Then a 4 is pressed and "00:14". A 2 is pressed and "01:42".
  2. I want to store this as an NSNumber on the controller as the number of seconds in the timer. So if the text field shows "10:00", then the value on the property would be 600.

Seems like NSFormatter is a good place to be looking, but my attempts have been full of fail. Any help is welcome. Thanks!

A: 

a) Divide the value of your timer property into minutes and seconds. Then use an NSFormatter setup with two leading zeros to create strings from the two numbers (see: How to specify decimal places when formatting NSNumber objects?). Then use stringWithFormat: to put the two strings on either side of a colon.

b) Or just divide out each digit of the four (in your example) numbers and put that number in a string with the colon in the correct spot. (so first the tens of minutes, then the minutes, then the tens of seconds, then the seconds)

Either of the above could be in a custom NSFormatter subclass set on the NSTextField or in a method in the view's controller called by an observer (using KVO) of your timer property.

Nathan Kinsinger