0

I already have a slider that controls the output of a text label.

-(IBAction)mySlider:(UISlider *)sender
{
    _myTextLabel.text = [NSString stringWithFormat:@"%i", (int) sender.value];
}

However, I also want a stepper to be able to changer the slider's position. So, if the slider has been set to 50, and the text label says 50, I want the stepper to increment to 51, 52, 53, etc. Does anyone know the code to do this? Thanks!

jake9115
  • 3,964
  • 12
  • 49
  • 78
  • What is your question? – rmaddy May 06 '13 at 03:49
  • So if I am reading this correctly, you're pretty much wanting the UISlider to update the label, and then the stepper to update the slider? – user2277872 May 06 '13 at 03:50
  • Yes, that is right, and my question is what is the code to do that... Basically, if the user is trying to select a specific number, they can use the slider as a coarse adjustment, and then use the incrementer to get the exact number – jake9115 May 06 '13 at 03:51

1 Answers1

3

You will need to keep an reference for the UISlider. Make a handler for UIStepper with valueChanged event.

Some settings that need to be done for both stepper and slider

Set minimum value and maximum value for both stepper and slider as 0 and 100 respectively. Set the step size of stepper as 1. Set the default value same for both stepper ans slider.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.slider.minimumValue = 0;
    self.slider.maximumValue = 100;

    self.stepper.minimumValue = 0;
    self.stepper.maximumValue = 100;
    self.stepper.stepValue = 1;

    self.slider.value = 10;
    self.stepper.value = 10;

}

- (IBAction)stepperValueChanged:(UIStepper *)sender {

    [self.slider setValue:sender.value];

    self.myTextLabel.text = [@((int)sender.value) stringValue];

}

- (IBAction)sliderValueChanged:(UISlider *)sender {

    [self.stepper setValue:sender.value];

    self.myTextLabel.text = [@((int)sender.value) stringValue];
}
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • Thanks for this code, but I have a quick newb question: how do I make a reference for the UISlider? Everytime I make a method for a slider, I can't reference them outside of their { } braces, and I don't know how else to point to them – jake9115 May 06 '13 at 03:59
  • all you have to do is within your .h file, do @property (nonatomic, retain) UISlider *; between the end and } and then in your .m file, right under the implementation type synthesize ; and that will create the getter/setter functions to use so you can use them beyond the {}. implementation and synthesize both @ in front of them – user2277872 May 06 '13 at 04:03
  • Just connect the controls from IB, give the controls a property with IBOutlet connect them. The above setting of stepper, min, max and step can be set from IB itself. – Anupdas May 06 '13 at 04:03
  • Note that if you want to keep your slider and stepper in sync that you will need to update the opposite one whenever one of them changes. (i.e. when the slider changes, update the stepper, and vice-versa.) – lnafziger May 06 '13 at 04:06
  • @jake9115 I have completely updated the answer. Please see it. – Anupdas May 06 '13 at 04:34