更改滑块值(iOS)时滑块更改为0

时间:2017-03-21 19:51:51

标签: ios objective-c xcode

我正在尝试增加/减少滑块,但是当我开始拖动滑块时,显示的值会立即变为0.为什么?这是我的代码(它们都不起作用):

 - (IBAction)FrontLapChanged:(id)sender {
int myInt = (int) self.FrontlapSlider.value;
self.FrontlapMeasure.text = [NSString stringWithFormat:@"%li", myInt];
 }
 - (IBAction)SidelapChanged:(id)sender {
float myFloat = self.SidelapSlider.value;
int value = (int)myFloat;
NSString *string = [NSString stringWithFormat:@"%d", value];
self.SidelapMeasure.text = string;
 }

1 个答案:

答案 0 :(得分:0)

如上所述,您需要设置滑块的最小值和最大值:

[self.SidelapSlider setMinimumValue:0.f];
[self.SidelapSlider setMaximumValue:100.f];

[self.FrontlapSlider setMinimumValue:0.f];
[self.FrontlapSlider setMaximumValue:100.f];

或者不将滑块值(默认情况下介于0和1之间)转换为int。这是因为转换为int将失去精度。例如0.5转换为int的浮点数为0。

 - (IBAction)FrontLapChanged:(id)sender {
CGFloat value = self.FrontlapSlider.value;
self.FrontlapMeasure.text = [NSString stringWithFormat:@"%f", value];
 }
 - (IBAction)SidelapChanged:(id)sender {
CGFloat value = self.SidelapSlider.value;
self.SidelapMeasure.text = [NSString stringWithFormat:@"%f", value];
 }