1

I am new to Objective-C and teaching myself. Right now I am trying to develop an interactive clock, but now I am stuck so I need your expertise. Right now it's formatted into decimals to the 2nd place. I was wondering if it was possible to format it to a colon instead of a decimal. Or am I just approaching this problem wrong. :(

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary    *)change context:(void *)context
{

if(object == _knobControl && [keyPath isEqualToString:@"value"]) {
    self.valueLabel.text = [NSString stringWithFormat:@"%.2f", _knobControl.value];
}
}
user3281743
  • 295
  • 1
  • 11
  • 1
    What units is _knobControl.value? Is it a duration or elapsed time? Do you want to show 90 minutes as "01:30"? See http://stackoverflow.com/questions/4046991/formatting-seconds-into-hhiiss, http://stackoverflow.com/questions/15122883/ios-format-string-into-minutes-and-seconds. –  Feb 28 '14 at 03:18

2 Answers2

1

I have not understood your question clearly, so following code maybe help you, but I'm not suer:

NSString *text = [NSString stringWithFormat:@"%.2f", _knobControl.value];
self.valueLabel.text = [text stringByReplacingOccurrencesOfString:@"." withString:@":"];
simalone
  • 2,768
  • 1
  • 15
  • 20
1

If you have, eg, minutes, and you want to display 90 minutes as 1:30, use modulo arithmetic.

int time = 90;
int hours = time / 60;
int minutes = time % 60;
NSString* text = [NSString stringWithFormat:@"%02d:%02d", hours, minutes];
Hot Licks
  • 47,103
  • 17
  • 93
  • 151