Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an array of strings for UISlider

I want to change a UILabel with strings based on an array. Here is what I have so far:

- (IBAction) sliderValueChanged:(UISlider *)sender {
   scanLabel.text = [NSString stringWithFormat:@" %.f", [sender value]];
    NSString *wholeText = @"Very Bad";
    self.scanLabel.text = wholeText;

}

Instead of just "Very Bad," I want different values to show "very Bad, Bad, Okay, Good, Very Good."

Where do I declare the NSMutableArray, and how do I implement it with the slider?

Update: Thank you all for your help! (I love stackoverflow)

like image 496
Adam Avatar asked Jun 28 '26 09:06

Adam


2 Answers

This is not compiler checked...but you may get some idea.

NSArray *texts=[NSArray arrayWithObjects:@"very Bad", @"Bad", @"Okay", @"Good", @"Very Good",nil];
NSInteger sliderValue=[sender value]; //make the slider value in given range integer one.
self.scanLabel.text=[texts objectAtIndex:sliderValue];
like image 118
Anoop Vaidya Avatar answered Jun 29 '26 23:06

Anoop Vaidya


You declare it in the same class as the above method.

Let's say it's called wholeTexts.

The code will look like this:

- (IBAction) sliderValueChanged:(UISlider *)sender {
    scanLabel.text = [NSString stringWithFormat:@" %.f", [sender value]];
    NSString *wholeText = [wholeTexts objectAtIndex:(wholeTexts.count - 1) * (int)(sender.value - sender.minimumValue)/(sender.maximumValue - sender.minimumValue)];
    self.scanLabel.text = wholeText;
}
like image 41
sqreept Avatar answered Jun 29 '26 22:06

sqreept



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!