Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Created April 23, 2012 04:37

Revisions

  1. joshdholtz revised this gist Jun 1, 2012. 2 changed files with 63 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions UIButtonBlock.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    //
    // UIButton+Block.h
    // BoothTag
    //
    // Created by Josh Holtz on 4/22/12.
    // Copyright (c) 2012 Josh Holtz. All rights reserved.
    //

    #define kUIButtonBlockTouchUpInside @"TouchInside"

    #import <UIKit/UIKit.h>

    @interface UIButton (Block)

    @property (nonatomic, strong) NSMutableDictionary *actions;

    - (void) setAction:(NSString*)action withBlock:(void(^)())block;

    @end
    44 changes: 44 additions & 0 deletions UIButtonBlock.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    //
    // UIButton+Block.m
    // BoothTag
    //
    // Created by Josh Holtz on 4/22/12.
    // Copyright (c) 2012 Josh Holtz. All rights reserved.
    //

    #import "UIButton+Block.h"

    #import "/usr/include/objc/runtime.h"

    @implementation UIButton (Block)

    static char overviewKey;

    @dynamic actions;

    - (void) setAction:(NSString*)action withBlock:(void(^)())block {

    if ([self actions] == nil) {
    [self setActions:[[NSMutableDictionary alloc] init]];
    }

    [[self actions] setObject:[block copy] forKey:action];

    if ([kUIButtonBlockTouchUpInside isEqualToString:action]) {
    [self addTarget:self action:@selector(doTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    }
    }

    - (void)setActions:(NSMutableDictionary*)actions {
    objc_setAssociatedObject (self, &overviewKey,actions,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

    - (NSMutableDictionary*)actions {
    return objc_getAssociatedObject(self, &overviewKey);
    }

    - (void)doTouchUpInside:(id)sender {
    void(^block)();
    block = [[self actions] objectForKey:kUIButtonBlockTouchUpInside];
    block();
    }
  2. joshdholtz created this gist Apr 23, 2012.
    19 changes: 19 additions & 0 deletions UIButton+Block.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    //
    // UIButton+Block.h
    // BoothTag
    //
    // Created by Josh Holtz on 4/22/12.
    // Copyright (c) 2012 Josh Holtz. All rights reserved.
    //

    #define kUIButtonBlockTouchUpInside @"TouchInside"

    #import <UIKit/UIKit.h>

    @interface UIButton (Block)

    @property (nonatomic, strong) NSMutableDictionary *actions;

    - (void) setAction:(NSString*)action withBlock:(void(^)())block;

    @end
    44 changes: 44 additions & 0 deletions UIButton+Block.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    //
    // UIButton+Block.m
    // BoothTag
    //
    // Created by Josh Holtz on 4/22/12.
    // Copyright (c) 2012 Josh Holtz. All rights reserved.
    //

    #import "UIButton+Block.h"

    #import "/usr/include/objc/runtime.h"

    @implementation UIButton (Block)

    static char overviewKey;

    @dynamic actions;

    - (void) setAction:(NSString*)action withBlock:(void(^)())block {

    if ([self actions] == nil) {
    [self setActions:[[NSMutableDictionary alloc] init]];
    }

    [[self actions] setObject:block forKey:action];

    if ([kUIButtonBlockTouchUpInside isEqualToString:action]) {
    [self addTarget:self action:@selector(doTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    }
    }

    - (void)setActions:(NSMutableDictionary*)actions {
    objc_setAssociatedObject (self, &overviewKey,actions,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

    - (NSMutableDictionary*)actions {
    return objc_getAssociatedObject(self, &overviewKey);
    }

    - (void)doTouchUpInside:(id)sender {
    void(^block)();
    block = [[self actions] objectForKey:kUIButtonBlockTouchUpInside];
    block();
    }
    8 changes: 8 additions & 0 deletions ViewController.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    ...

    [button setAction:kUIButtonBlockTouchUpInside withBlock:^{
    NSString* launchUrl = @"http://joshholtz.com";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
    }];

    ..