如何让UISlider步进小数

时间:2013-09-21 21:03:07

标签: ios objective-c nsstring stringwithformat

我有一个最小的UISlider - 0.1& max - 1.我希望显示值的标签显示小数而不只是0和1。

以下是当前代码:

- (IBAction)sliderChange:(UISlider *)sender { // When the slider for the power factor is moved
sender.minimumValue = 0.1;
sender.maximumValue = 1.0;
int progress = lround(sender.value);
self.sliderLabel.text = [NSString stringWithFormat:@"%d", progress];

}

如果有人可以提供帮助,我会非常感激。

1 个答案:

答案 0 :(得分:0)

两件事:你的progress变量是一个int。将其更改为CGFloat

接下来,尝试使用

[NSString stringWithFormat:@"%f", progress]

代替(注意%f而不是%d)。 %f用于浮点值,%d用于整数。

这是官方[NSString stringWithFormat] formatting docs