Reputation: 932
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.
Slider should reprecent the values of the key "Price" in the dictionaries. I chose Number for it, so some of them are like 78,900000000001. In other words they need format with two decimals.
minimumValue = the lowest value in the dictionaries, rounded down to closest 10 (if lowest value = 67,90 then minimumValue = 60) if that's not too complicated.
maximumValue = highest value but rounded up to closest 10.
It is for user to set an optional minimum price, so default position should be minimumValue.
I want it to step by 5 numbers at the time when slided, like 70, 75, 80, 85, 90 etc
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
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