问题
...ViewController.h:
@property (nonatomic, strong) IBOutlet UISlider *slider;
...ViewController.m:
@synthesize slider;
- (void)viewDidLoad
{
[super viewDidLoad];
...
currentValue = 50;
self.slider.value = currentValue;
// Do any additional setup after loading the view, typically from a nib.
}
.xib:
The UISlider in my interface is connected to the slider property.
Behavior:
The slider sits all the way to the right at value 100 when it is supposed to be in the middle. An action I set up confirms the value of currentValue is 50. If currentValue is 50 and slider.value is set to 50, why doesn't the position reflect this?
P.S. I tried [slider setNeedsDisplay] and it did not affect this behavior.
Update: thanks for the quick answers! What a silly thing to do. I print out currentValue as an int elsewhere so I am now setting slider.value with this code:
self.slider.value = (float) currentValue/100;
回答1:
currentValue = 50;
The slider's range is not 0...100 but 0...1. You have to write either
currentValue = 0.5;
or change the range of the slider:
self.slider.maximumValue = 100;
回答2:
Default range for UISlider is 0.0 to 1.0 (i.e. as a Float). Setting a value above max will cause the slider to show all the way to the right. Check your minimumValue and maximumValue properties.
来源:https://stackoverflow.com/questions/12322562/why-doesnt-setting-the-value-property-of-this-uislider-update-its-position