Reputation: 10244
I have a UISlider which I'd like to allow a selection between 0.5 and 10. When the callback is fired to set the value of a label when the value changes, I get a strange range of values. The code I have is:
-(IBAction)DidChangeSliderValue:(id)sender {
NSString *distance = [NSString stringWithFormat:@"%i miles", sdrDistance.value];
lblDistance.text = distance;
NSLog(distance);
I have the slider set up as follows:
However, dragging from left to right results in this to the log:
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
Is there any logical reason for this?
Upvotes: 0
Views: 223
Reputation: 36
You are assigning an int (%i) to a float value (%f) because a uislider is a float (decimals) not an int (whole number) So it should be this:
-(IBAction)DidChangeSliderValue:(id)sender {
NSString *distance = [NSString stringWithFormat:@"%f miles", sdrDistance.value];
lblDistance.text = distance;
NSLog(distance);
Upvotes: 0
Reputation: 53551
[UISlider value]
returns a float
, not an int
, so you have to use %f
instead of %i
in your NSLog statement.
Upvotes: 1