ingenspor
ingenspor

Reputation: 932

Populate a UISlider with values

I'm making a search function in my app but I'm having some trouble figuring the codes for my UISlider. Slider is created in the storyboard and hooked up. I have a .plist with an array of dictionaries as info source. The dictionaries are containing info about wines.



This is the codes for the UISlider so far:

- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = @"Search";

NSString *path = [[NSBundle mainBundle] pathForResource:@"Wines" ofType:@"plist"];
allObjectsArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

minSlider.continuous = YES;
[minSlider setMinimumValue: ]; // Lowest value rounded down to closest 10
[minSlider setMaximumValue: ]; // Highest value rounded up to closest 10

[minSlider addTarget:self
           action:@selector(minValueChanged:) 
 forControlEvents:UIControlEventValueChanged];

minText.text = minSlider.minimumValue // <--- or something like that
}

- (void)minValueChanged:(UISlider*)sender
{
 // And something here for change of value and text field
minText.text = current slider value
}

Can you help me finish this? I've started up based on what I learned from my research before asking. :)

Upvotes: 0

Views: 305

Answers (1)

Mundi
Mundi

Reputation: 80265

It depends a bit on how your plist is structured, but you are not telling us... I am assuming it looks something like

<array>
  <dict>
    <key>name</key>
    <string>Bordeaux</string>
    <key>price</key>
    <real>75.40</real>
  </dict>
  <dict>
    <key>name</key>
    <string>Chardonnay</string>
    <key>price</key>
    <real>29.90</real>
  </dict>
</array>

This is how you determine the minimum and maximum values:

NSNumber *minValue = [allObjectsArray valueForKeyPath:@"@min.price"];
NSNumber *maxValue = [allObjectsArray valueForKeyPath:@"@max.price"];

This is how you round the numbers:

float roundedDown = ((int)[minValue floatValue]/10)*10.0;
float roundedUp = ((int)[maxValue floatValue]/10)*10.0 +10;

This is how you set the slider minimum value:

minSlider.minimumValue = [minValue floatValue];

This is how you make a label display only multiples of 5:

-(void)minValueChanged:(UISlider*)sender {
    minText.text = [NSString stringWithFormat:@"%.0f", 
       5.0 * ((int) (sender.value / 5) + 0.5)];
}

Upvotes: 1

Related Questions