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:
[button handleControlEvents:UIControlEventTouchUpInside withBlock:^() {
NSLog(@"button pressed: %@", [button titleForState:UIControlStateNormal]);
}];
You can then remove the blocks with:
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.
- Objective-C UIControl Category Inspired By JQuery Adding Many Blocks-Based Events
- Open Source: Massive Collection Of Objective-C Blocks Extensions
- NSDate Add-On Category For Easily Obtaining Locale Adjusted Date Information
- Tutorial: Objective-C Blocks (Closures) For Beginners
- Handy Cheat Sheet For Using Objective-C Blocks (Closures)
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.