UISlider 产生奇怪的值
我有一个 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
这有什么逻辑原因吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[UISlider value]
返回一个float
,而不是int
,因此您必须使用%f
而不是 < NSLog 语句中的 code>%i 。[UISlider value]
returns afloat
, not anint
, so you have to use%f
instead of%i
in your NSLog statement.值
是一个浮点数。使用%f
而不是%i
。The
value
is a float. Use%f
instead of%i
.您将 int (%i) 分配给 float 值 (%f),因为 uislider 是 float(小数)而不是 int(整数)所以它应该是这样的:
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: