miholzi miholzi - 3 months ago 10
Objective-C Question

Set slider value

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

Thanks!




here my code:

ViewController.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

#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;
}

Answer

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%).

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.