Skip to content

Instantly share code, notes, and snippets.

@pk
Created January 8, 2016 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pk/4be84591d84fc297f9ee to your computer and use it in GitHub Desktop.
Save pk/4be84591d84fc297f9ee to your computer and use it in GitHub Desktop.
- (IBAction)buttonTapped:(UIButton *)aSender {
self.validationErrors = nil;
// Translate Button -> Option
NSUInteger index = [self.buttonsView.subviews indexOfObject:aSender];
PQMarkSheetAnswer *selectedOption = (self.options)[index];
// When we have single choice we need to toggle options
if (!self.allowsMultipleSelection && [self.selectedOptions count] > 0) {
PQMarkSheetAnswer *alreadySelectedOption = [self.selectedOptions firstObject];
NSUInteger selectedOptionIndex = [self.options indexOfObject:alreadySelectedOption];
if (selectedOptionIndex == NSNotFound) {
NSDictionary *results = [[[(PQMarkingSheetViewController *)self.delegate interview] result] results];
DDLogError(@"PRAC-711 Occured!\n"
"Cell/Question: %@\n"
"Options: %@\n"
"SelectedOption: %@\n"
"SelectedOptions: %@\n"
"Buttons: %@\n"
"Touched button: %@\n"
"Results: %@", self.label.text, self.options, selectedOption, self.selectedOptions, self.buttonsView.subviews, aSender, results);
// We remove the option we can't find in options from selected options
// and just continue lower with selecting/deselecting
[self.selectedOptions removeObject:alreadySelectedOption];
}
// Toggle only if the answer is not the same, otherwise it will be handled lower
else if (alreadySelectedOption != selectedOption) {
UIButton *alreadySelectedButton =
[self.buttonsView subviews][selectedOptionIndex];
alreadySelectedButton.selected = NO;
[self.selectedOptions removeObject:alreadySelectedOption];
}
}
// Allow option to be selected/deselected
BOOL alreadySelected =
[self.selectedOptions indexOfObject:selectedOption] != NSNotFound;
if (alreadySelected) {
aSender.selected = NO;
[self.selectedOptions removeObject:selectedOption];
[self.delegate questionCell:self didUpdateWithAnswers:self.selectedOptions];
} else {
aSender.selected = YES;
[self.selectedOptions addObject:selectedOption];
[self.delegate questionCell:self didUpdateWithAnswers:self.selectedOptions];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment