UISlider 产生奇怪的值

发布于 2024-11-19 10:32:25 字数 1321 浏览 3 评论 0原文

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

-(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

这有什么逻辑原因吗?

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:
Slider Setup

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我爱人 2024-11-26 10:32:25

[UISlider value] 返回一个 float,而不是 int,因此您必须使用 %f 而不是 < NSLog 语句中的 code>%i 。

[UISlider value] returns a float, not an int, so you have to use %f instead of %i in your NSLog statement.

我最亲爱的 2024-11-26 10:32:25

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

The value is a float. Use %f instead of %i.

≈。彩虹 2024-11-26 10:32:25

您将 int (%i) 分配给 float 值 (%f),因为 uislider 是 float(小数)而不是 int(整数)所以它应该是这样的:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文