Set the slider value
I'm trying to set the value of a slider after a button is clicked but it doesn't work - the slider doesn't move. the code works fine with no errors.
It should be pretty simple, but I can't figure it out. xCode Version 4.5.2
Thank!
here's 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;
}
source to share
I think your problem is that the slider takes 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 90 (if you mean 90%).
If the slider is updating but not animating, use:
[_slider setValue:0.9 animated:YES];
Note that given your code, you may need self.slider
instead _slider
.
source to share
Check Answer
- (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;
}
source to share
To use slider.value = 90;
Your ViewController.h should be:
#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.
Otherwise use self.slider.value = 90;
or_slider.value = 90;
source to share
Since you are using the interface designer, the first thing to look out for is the UI components (slider, button) connected to the IBOutlets in the file owner. This is the most common source of such problems.
Then check the min and max of the slider (default 0; 1 - as indicated by rdurand).
And spider1983 is also correct: since the slider is a property, you can refer to it as self.slider (or _slider, but in this case you shouldn't use the latter).
source to share
I think. since you did not specify the maximum and minimum value of the slider, it assumes the default max and min values ββof 0: 1. So it is not possible to set value = 90. First, change its max and min as follows:
Slider.minimumValue = 1;
Slider.maximumValue = 100;
try your business,
Slider.value =90;
source to share