user2732294
user2732294

Reputation: 641

Objective C - How to change the UIslider value with button Click actions

Slide with +, - Buttons

As show in the figure above I have 1 button to decrease and 1 button to increase the value of the slider.

The range of the slider is 1 to 10.

My requirement is to change slider's value 0.5 by each button click.

For example if the slider's current value is 4, clicking the + should change the value to 4.5 and change the display accordingly.

Same thing with negative button to change value to 3.5.

How can I accomplish this?

Upvotes: 1

Views: 1825

Answers (4)

Erinson
Erinson

Reputation: 88

for iOS 11 Objective C

- (IBAction)Plus:(id)sender {
    [self.mySlider setValue:self.mySlider.value+1];
    NSInteger index = mySlider.value;
    [self.imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [imagenArray objectAtIndex:index]]]];
    [_counter setText:[NSString stringWithFormat:@"%d/%lu", index+1, (unsigned long)imagenArray.count]];

 }

- (IBAction)Menus:(id)sender {
    [self.mySlider setValue:self.mySlider.value-1];
    NSInteger index = mySlider.value;
    [self.imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [imagenArray objectAtIndex:index]]]];
    [_counter setText:[NSString stringWithFormat:@"%d/%lu", index-1, (unsigned long)imagenArray.count]];

 }

Upvotes: 0

Antonio MG
Antonio MG

Reputation: 20410

The steps are these:

  • Create a class variable with your UISlider, and set the min and the max values of it

  • Add a target to the buttons. When the button is pressed, do:

[self.mySlider setValue:self.mySlider.value + 0.5];

Upvotes: 1

Lord Zsolt
Lord Zsolt

Reputation: 6557

Create an outlet for the slider if it's created using Interface builder.

Then just do "mySliderOutletName.value += customValue";

Add the above code to the Action triggered by touch up inside for the UIButton.

If it's created from code, add it to the class interface, then you can refer to it in all the methods of the class.

There's a good video tutorial about UISlider here.

Upvotes: 1

Adnan Aftab
Adnan Aftab

Reputation: 14477

On tapPlusButton change the value of slider by adding 0.5 to current value like this.

[self.slider setValue:self.slider.value+0.5];

Upvotes: 2

Related Questions