iOS UIControl Category That Allows You To Handle Events Using Blocks

I previously mentioned a library that adds many different block based events to several UIControl objects.

Here’s a UIControl category from Andy Lavoy called ALActionBlocks that allows you to easily use blocks for UIControl events where you would normally supply an @selector method.

As shown in the readme for ALActionBlocks you can add a block to a UIControl with:

// Assuming you have a UIButton named ‘button’
[button handleControlEvents:UIControlEventTouchUpInside withBlock:^() {
NSLog(@"button pressed: %@", [button titleForState:UIControlStateNormal]);
}];

You can then remove the blocks with:

[button removeActionBlocksForControlEvents:UIControlEventTouchUpInside];

You can find ALActionBlocks on Github here.

You can read this answer on Stackoverflow that explains the technique used to pass a block for the @selector used by the UIControl.

DeliciousTwitterFacebookRedditLinkedInEmail

Original article: iOS UIControl Category That Allows You To Handle Events Using Blocks

©2013 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *