Last active
January 5, 2022 09:38
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The description is about as useless as the gist is valuable. See button_group.m for sample use.