Skip to content

seivan/SHControlBlocks

Repository files navigation

Deprecated, please use RxCocoa

SHControlBlocks

CI Status Version Platform License

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.

Overview

The blocks and events are automatically removed once their control is gone, so it isn't necessary to clean up - Swizzle Free(™)

API

USAGE

Installation

pod 'SHControlBlocks'

Setup

Put this either in specific files or your project prefix file

#import "UIControl+SHControlBlocks.h"

or

#import "SHControlBlocks.h"

API

Creating

#pragma mark -
#pragma mark Add block
-(void)SH_addControlEvents:(UIControlEvents)controlEvents
                 withBlock:(SHControlEventBlock)theBlock;

-(void)SH_addControlEventTouchUpInsideWithBlock:(SHControlEventBlock)theBlock;

Removing

#pragma mark -
#pragma mark Remove block
-(void)SH_removeControlEventTouchUpInside;
-(void)SH_removeBlocksForControlEvents:(UIControlEvents)controlEvents;
-(void)SH_removeControlEventsForBlock:(SHControlEventBlock)theBlock;
-(void)SH_removeAllControlEventsBlocks;

Helpers and Properties

#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;

Usage

Creating

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 :)
  }];

Removing

Remove specific blocks - will also remove the Event from the target if it was the last block

  [btnSecond SH_removeControlEventsForBlock:counterBlock];

Remove specific events

  [btnSecond SH_removeBlocksForControlEvents:UIControlEventTouchUpInside];
  [btnSecond SH_removeControlEventTouchUpInside];

Remove all blocks and events

  [button SH_removeAllControlEventsBlocks];

Helpers and Properties


  [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");

Contact

If you end up using SHControlBlocks in a project, I'd love to hear about it.

email: seivan.heidari@icloud.com
twitter: @seivanheidari

License

SHControlBlocks is © 2013 Seivan and may be freely distributed under the MIT license. See the LICENSE.md file.

About

Prefixed UIControl category with blocks, auto-removed. Swizzle free!

Resources

License

Stars

Watchers

Forks

Packages

No packages published