Skip to content

Instantly share code, notes, and snippets.

@theoknock
Last active January 4, 2022 05:54
Show Gist options
  • Save theoknock/4d24c3c87848fcd7a9ba70c6340f72f3 to your computer and use it in GitHub Desktop.
Save theoknock/4d24c3c87848fcd7a9ba70c6340f72f3 to your computer and use it in GitHub Desktop.
Button group (aka radio buttons). Creates an array of reusable UIButton objects, which, in this case, are retrievable by an enumerated property that corresponds to the index in the array. This allows each button to access the entire group of buttons, even in their respective event handlers (so, for example, if one button is selected, it could de…
static void (^print_debug)(const char *) = ^ (const char * str) {
static int counter;
printf("\n%d\t%s\n", ++counter, str);
};
static const UIButton * (^buttons[5])(void);
static const UIButton * (^(^(^button_group)(CaptureDeviceConfigurationControlPropertyBitMask))(CaptureDeviceConfigurationControlProperty))(void) = ^ (CaptureDeviceConfigurationControlPropertyBitMask property_bit_mask) {
print_debug("INIT BUTTONS");
for (int property_tag = 0; property_tag < 5; property_tag++) {
__block UIButton * (^button)(void);
button = ^{
print_debug("\t\tINIT BUTTON");
UIButton * button;
[button = [UIButton new] setTag:property_tag];
[button setBackgroundColor:[UIColor clearColor]];
[button setImage:[UIImage systemImageNamed:CaptureDeviceConfigurationControlPropertyImageNames[0][property_tag] withConfiguration:CaptureDeviceConfigurationControlPropertySymbolImageConfiguration(CaptureDeviceConfigurationControlStateDeselected)] forState:UIControlStateNormal];
[button setImage:[UIImage systemImageNamed:CaptureDeviceConfigurationControlPropertyImageNames[1][property_tag] withConfiguration:CaptureDeviceConfigurationControlPropertySymbolImageConfiguration(CaptureDeviceConfigurationControlStateSelected)] forState:UIControlStateSelected];
[button sizeToFit];
[button setUserInteractionEnabled:TRUE];
void (^eventHandlerBlock)(void) = ^{
print_debug("");
for (int property = 0; property < 5; property++) {
[buttons[property]() setSelected:(property == button.tag)];
};
};
objc_setAssociatedObject(button, @selector(invoke), eventHandlerBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[button addTarget:eventHandlerBlock action:@selector(invoke) forControlEvents:UIControlEventTouchUpInside];
return ^ UIButton * (void) {
print_debug("");
return button;
};
}();
buttons[property_tag] = button;
}
print_debug("");
return ^ (CaptureDeviceConfigurationControlProperty property_index) {
print_debug("");
return buttons[property_index];
};
};
static UIButton * (^(^property_buttons)(CaptureDeviceConfigurationControlProperty))(void);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment