Skip to content

Instantly share code, notes, and snippets.

@theoknock
Last active January 5, 2022 09:38
Show Gist options
  • Save theoknock/af99f829282a487234f67d9d8e7200e3 to your computer and use it in GitHub Desktop.
Save theoknock/af99f829282a487234f67d9d8e7200e3 to your computer and use it in GitHub Desktop.
Blocks as target/action selectors for UIControlEvents (see my other gist, button_group.m, for an example implementation). Insert this code into any UIButton initializer, whether subclassing or otherwise, to add this event handler.
void (^eventHandlerBlock)(void) = ^{
printf("\nHandling event for button %lu\n", some_local_variable);
};
objc_setAssociatedObject(button, @selector(invoke), eventHandlerBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[button addTarget:eventHandlerBlock action:@selector(invoke) forControlEvents:UIControlEventAllEvents];
@theoknock
Copy link
Author

The description is about as useless as the gist is valuable. See button_group.m for sample use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment