I have 4 UISliders that need to have values from 1-5 with 1 being the minimum value and 5 the maximum.
I have set up the sliders and call them slider1, slider2, slide3, slider4
I have also set up the outlets etc and set the minimum and maximum values in interface builder
However when I scroll the slider it just slides instead of sticking to the 1-5 scale.
I have attempted the following and am failing miserably as I saw this at some other SO question
- (IBAction) sliderValueChanged:(id)sender
{
[sender setValue:((int)((sender getValue] + 2.5) / 5) * 5) animated:NO];
}
UPDATED: This seems to work as a solution
- (IBAction) sliderValueChanged:(UISlider *)sender
{
NSUInteger index = (NSUInteger)([sender value] + 0.5); // Round the number.
[sender setValue:index animated:NO];
NSLog(@"index: %i", index);
// NSNumber *number = [numbers objectAtIndex:index]; // <-- This is the number you want.
// NSLog(@"number: %@", number);
}
can you help me with this? I am getting an error with the getValue function also above.
Objective-C ain’t Java, the naming convention for getters is different. Use
valueinstead ofgetValue(and anyways, why don’t you declaresenderasUISlider *?)