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

      

+3


source to share


7 replies


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

.

+7


source


try using

    self.slider.value = 90;

      



instead

    slider.value = 90;

      

+3


source


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

      

+1


source


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;

+1


source


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

0


source


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;

      

0


source


For Swift, you can use:

slider.setValue(5.0, animated: true)

      

0


source







All Articles