I’d like to have a slider (lets say values from 0 to 4) and an according label which content does NOT display the current slider value BUT an according string.
I’m quite new to Objective-C but here is what I tried (…and did not work 😉 )
first I made an Array:
sliderArray = [[NSArray alloc] initWithObjects:
@"sad" @"not so good" @"average" @"good" @"awesome", nil];
then I defined the action:
(IBAction)sliderChange:(id)sender {
UISlider *slider = (UISlider *) sender;
NSString *mood = [[NSString alloc] init];
mood = @"%@", [sliderArray objectAtIndex:(int)[slider value]];
[sliderLabelMood setText:mood];
}
Xcode can compile but as soon as I try to move the slider it crashes.
The problem is your assignment to the
NSStringvariablemood.If you use a format string you will have to use the
NSStringclass methodstringWithFormat:thus eitheror
Also, you should make sure that the slider value really returns an
NSInteger. The normal type for a slider’svalueproperty isfloat. Thus: