I currently have a UISwitch that when turned on and off increments and decrements a counter respectively.
When the counter is at 0 the counter will not decrement. Functionally, this works perfectly however I have noticed a bug and wondered if anyone has experienced this.
Essentially if you very very quickly double tap the UISwitch at its far position (fully on or off) the counter will increment twice as I'm imagining the UISwitch isn't fully hitting the off state and is therefore simply adding to the counter again without first decrementing it.
Here's the code I'm using to do the checks on the switch:
// Sliders modified
- (IBAction)personalityChanged:(id)sender {
if ([personality isOn] ){
[[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"1"];
rating ++;
NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]);
[personality set]
}
else {
[[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"0"];
[self subtractFromRating:nil];
NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]);
}
[self checkRating:nil];
}
Then the subtract rating:
// subtract from rating
-(void)subtractFromRating:(id)sender{
if (rating == 0) {
// do nothing
}
else
{
rating --;
}
}
And finally the result of what happens if the slider is in a position:
// check rating
-(void)checkRating:(id)sender{
switch (rating) {
case 0:
[matchRating setText:@""];
[ratingGraphic setImage:[UIImage imageNamed:@""]];
NSLog(@"rating is 0");
break;
case 1:
[matchRating setText:@"Single Match"];
[ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic1.png"]];
NSLog(@"rating is 1");
break;
case 2:
[matchRating setText:@"Potential Match"];
[ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic2.png"]];
NSLog(@"rating is 2");
break;
case 3:
[matchRating setText:@"Great Match"];
[ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic3.png"]];
NSLog(@"rating is 3");
break;
case 4:
[matchRating setText:@"Hot Match"];
[ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic4.png"]];
NSLog(@"rating is 4");
break;
default:
break;
}
}
Is there a way to make sure the switch goes from the on state to the off fully before returning or a better approach for this?
A solution to detect if there has actually been a change is to keep an additional BOOL variable that keeps track of the last switch state.
BOOL lastValue = NO; // initial switch state
- (IBAction)personalityChanged:(id)sender {
if (personality.isOn != lastValue) {
lastValue = personality.isOn;
if ([personality isOn] ){
[[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"1"];
rating ++;
NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]);
[personality set]
}
else {
[[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"0"];
[self subtractFromRating:nil];
NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]);
}
[self checkRating:nil];
}
}
This will then only perform when the switch state has actually changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With