我有一个UISlider,我想允许在0.5和10之间进行选择。当回调被触发以在值改变时设置标签的值时,我得到一个奇怪的值范围。我的代码是:
-(IBAction)DidChangeSliderValue:(id)sender {
NSString *distance = [NSString stringWithFormat:@"%i miles", sdrDistance.value];
lblDistance.text = distance;
NSLog(distance);
我将滑块设置如下:
但是,从左向右拖动会导致日志:
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
这有什么合理的理由吗?
答案 0 :(得分:1)
value
是一个浮点数。使用%f
代替%i
。
答案 1 :(得分:1)
[UISlider value]
会返回float
,而不是int
,因此您必须在NSLog语句中使用%f
而不是%i
。
答案 2 :(得分:0)
您正在为浮点值(%f)分配一个int(%i),因为uislider是一个浮点数(小数)而不是一个int(整数)所以它应该是这样的:
-(IBAction)DidChangeSliderValue:(id)sender {
NSString *distance = [NSString stringWithFormat:@"%f miles", sdrDistance.value];
lblDistance.text = distance;
NSLog(distance);