Deprecated, please use RxCocoa
This pod is used by
SHUIKitBlocks
as part of many components covering to plug the holes missing from Foundation, UIKit, CoreLocation, GameKit, MapKit and other aspects of an iOS application's architecture.
The blocks and events are automatically removed once their control is gone, so it isn't necessary to clean up - Swizzle Free(™)
pod 'SHControlBlocks'
Put this either in specific files or your project prefix file
#import "UIControl+SHControlBlocks.h"
or
#import "SHControlBlocks.h"
#pragma mark -
#pragma mark Add block
-(void)SH_addControlEvents:(UIControlEvents)controlEvents
withBlock:(SHControlEventBlock)theBlock;
-(void)SH_addControlEventTouchUpInsideWithBlock:(SHControlEventBlock)theBlock;
#pragma mark -
#pragma mark Remove block
-(void)SH_removeControlEventTouchUpInside;
-(void)SH_removeBlocksForControlEvents:(UIControlEvents)controlEvents;
-(void)SH_removeControlEventsForBlock:(SHControlEventBlock)theBlock;
-(void)SH_removeAllControlEventsBlocks;
#pragma mark -
#pragma mark Helpers
-(NSSet *)SH_blocksForControlEvents:(UIControlEvents)theControlEvents;
-(NSSet *)SH_controlEventsForBlock:(SHControlEventBlock)theBlock;
#pragma mark -
#pragma mark Properties
#pragma mark -
#pragma mark Getters
@property(nonatomic,readonly) BOOL SH_isTouchUpInsideEnabled;
@property(nonatomic,readonly) NSDictionary * SH_controlBlocks;
With SHControlBlocks you can set auto-removed blocks instead of using selectors
[self.btnFirst SH_addControlEvents:UIControlEventTouchDown withBlock:^(UIControl *sender) {
[weakSelf performSegueWithIdentifier:@"second" sender:nil];
NSLog(@"first");
}];
or if you want add additional blocks
[btnSecond SH_addControlEvents:UIControlEventTouchUpInside withBlock:counterBlock];
[btnSecond SH_addControlEvents:UIControlEventTouchDown withBlock:counterBlock];
Convenience selector for touchUpInside
[button SH_addControlEventTouchUpInsideWithBlock:^(UIControl *sender) {
[button removeFromSuperview]; //this will also remove the block :)
}];
[btnSecond SH_removeControlEventsForBlock:counterBlock];
[btnSecond SH_removeBlocksForControlEvents:UIControlEventTouchUpInside];
[btnSecond SH_removeControlEventTouchUpInside];
[button SH_removeAllControlEventsBlocks];
[button SH_addControlEventTouchUpInsideWithBlock:blockOne];
[button SH_addControlEventTouchUpInsideWithBlock:blockTwo];
[button SH_addControlEventTouchUpInsideWithBlock:blockThree];
NSSet * controlBlocks = button.SH_controlBlocks[@(UIControlEventTouchUpInside)];
NSAssert(button.SH_isTouchUpInsideEnabled, @"Touch up inside should be enabled");
NSAssert(button.SH_controlBlocks.count == 1, @"There should be one event");
NSAssert(controlBlocks.count == 3, @"There should be three blocks");
If you end up using SHControlBlocks in a project, I'd love to hear about it.
email: seivan.heidari@icloud.com
twitter: @seivanheidari
SHControlBlocks is © 2013 Seivan and may be freely
distributed under the MIT license.
See the LICENSE.md
file.