ios 设置滑块值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14455095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set slider value
提问by miholzi
i try to set a value to a slider after a button click, but it does not work - the slider does not move. the code runs fine without mistakes.
我尝试在单击按钮后为滑块设置一个值,但它不起作用 - 滑块不移动。代码运行良好,没有错误。
It should be pretty simple but I can't figure it out. xCode Version 4.5.2
它应该很简单,但我无法弄清楚。xCode 版本 4.5.2
Thanks!
谢谢!
here my code:
这是我的代码:
ViewController.h
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *debugLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;
- (IBAction)slider:(id)sender;
- (IBAction)ButtonChangeValue:(id)sender;
@end
ViewController.m
视图控制器.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize slider;
...
- (IBAction)slider:(UISlider *)sender {
float value = [sender value];
self.debugLabel.text = [NSString stringWithFormat:@"Value: %f",value];
}
- (IBAction)ButtonChangeValue:(id)sender {
slider.value = 90;
}
回答by rdurand
I think your problem is that the slider accepts values between 0.0 and 1.0 (default values, change them with _slider.maximumValue
and _slider.minimumValue
). Try setting the value to 0.9 instead of setting 90 (if you mean 90%).
我认为您的问题是滑块接受 0.0 和 1.0 之间的值(默认值,使用_slider.maximumValue
和更改它们_slider.minimumValue
)。尝试将该值设置为 0.9 而不是设置 90(如果您的意思是 90%)。
If the slider updates but is not animated, use :
如果滑块更新但没有动画,请使用:
[_slider setValue:0.9 animated:YES];
Note that given your code, you may need to use self.slider
instead of _slider
.
请注意,鉴于您的代码,您可能需要使用self.slider
代替_slider
.
回答by spider1983
try using
尝试使用
self.slider.value = 90;
instead of
代替
slider.value = 90;
回答by iPatel
Check MyAnswer
检查我的答案
- (void)viewDidLoad
{
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
[slider addGestureRecognizer:gr];
}
- (void)sliderTapped:(UIGestureRecognizer *)g
{
UISlider* s = (UISlider*)g.view;
if (s.highlighted)
return; // tap on thumb, let slider deal with it
CGPoint pt = [g locationInView: s];
CGFloat percentage = pt.x / s.bounds.size.width;
CGFloat delta = percentage * (s.maximumValue - s.minimumValue);
CGFloat value = s.minimumValue + delta;
[s setValue:value animated:YES];
NSString *str=[NSString stringWithFormat:@"%.f",[self.slider value]];
self.lbl.text=str;
}
回答by MuhammadBassio
To use slider.value = 90;
使用 slider.value = 90;
Your ViewController.h should be:
你的 ViewController.h 应该是:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UISlider *slider;
}
@property (weak, nonatomic) IBOutlet UILabel *debugLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;
- (IBAction)slider:(id)sender;
- (IBAction)ButtonChangeValue:(id)sender;
@end
And don't forget @synthesize slider;
in your ViewController.m as well.
并且不要忘记@synthesize slider;
在您的 ViewController.m 中。
Otherwise, use self.slider.value = 90;
or _slider.value = 90;
否则,使用self.slider.value = 90;
或_slider.value = 90;
回答by Abhirajsinh Thakore
For Swift, you can use:
对于 Swift,您可以使用:
slider.setValue(5.0, animated: true)
回答by Rana
i think . as you have not mentioned maximum and minimum value of slider ,it is taking default max and min values that are 0 to 1. So it is not possible to set value =90. First change its max and min like this :
我认为 。由于您没有提到滑块的最大值和最小值,它采用 0 到 1 的默认最大值和最小值。因此无法设置值 = 90。首先像这样改变它的最大值和最小值:
Slider.minimumValue = 1;
Slider.maximumValue = 100;
now try your thing ,
现在试试你的东西,
Slider.value =90;
回答by Rok Jarc
Since you are using interface builder first thing to look at would be: are the UI components (slider, button) connected to the IBOutlets in File's owner. This is the most common source of problems like this.
由于您使用的是界面构建器,因此首先要查看的是:UI 组件(滑块、按钮)是否连接到文件所有者中的 IBOutlets。这是此类问题的最常见来源。
Then check the min and max of the slider (default is 0;1 - as rdurand mentioned).
然后检查滑块的最小值和最大值(默认值为 0;1 - 正如 rdurand 所述)。
And spider1983 is correct also: since slider is a property, you can address it as self.slider (or _slider, but you shouldn't use the last one in this case).
Spider1983 也是正确的:因为滑块是一个属性,你可以将它作为 self.slider(或 _slider,但在这种情况下你不应该使用最后一个)。