2011-07-12 156 views
0

我有一个UISlider,我希望允许在0.5和10之间进行选择。当值更改时触发设置标签值的回调函数时,会得到一个奇怪的值范围。我的代码是:UISlider生成奇怪的值

-(IBAction)DidChangeSliderValue:(id)sender { 
    NSString *distance = [NSString stringWithFormat:@"%i miles", sdrDistance.value]; 
    lblDistance.text = distance; 
    NSLog(distance); 

我所立为滑块如下: Slider Setup

然而,由左到这个正确的结果到日志拖动:

1-07-12 16:38:24.354 myapp[2758:207] 1073741824 miles 
2011-07-12 16:38:24.355 myapp[2758:207] 0 miles 
2011-07-12 16:38:24.356 myapp[2758:207] 0 miles 
2011-07-12 16:38:27.873 myapp[2758:207] 0 miles 
2011-07-12 16:38:27.890 myapp[2758:207] -1073741824 miles 
2011-07-12 16:38:27.924 myapp[2758:207] -2147483648 miles 
2011-07-12 16:38:27.941 myapp[2758:207] 536870912 miles 
2011-07-12 16:38:27.957 myapp[2758:207] -2147483648 miles 
2011-07-12 16:38:27.991 myapp[2758:207] 1073741824 miles 
2011-07-12 16:38:28.007 myapp[2758:207] -536870912 miles 
2011-07-12 16:38:28.023 myapp[2758:207] 1073741824 miles 
2011-07-12 16:38:28.041 myapp[2758:207] 536870912 miles 
2011-07-12 16:38:28.058 myapp[2758:207] -536870912 miles 
2011-07-12 16:38:28.074 myapp[2758:207] 1073741824 miles 
2011-07-12 16:38:28.091 myapp[2758:207] 536870912 miles 
2011-07-12 16:38:28.141 myapp[2758:207] 0 miles 

是这有什么合乎逻辑的原因?

回答

1

[UISlider value]返回float,不是int,所以你必须在你的NSLog语句中使用%f而不是%i

1

value是一个浮点数。使用%f而不是%i

0

您分配一个int(%I)为浮点值(%F),因为uislider是浮点(小数)不是一个int(整数),所以它应该是这样的:

-(IBAction)DidChangeSliderValue:(id)sender { 
NSString *distance = [NSString stringWithFormat:@"%f miles", sdrDistance.value]; 
lblDistance.text = distance; 
NSLog(distance);