views:

58

answers:

1

I looked around, but couldn't find this on the internet, nor anywhere in the Apple docs, so I'm guessing it doesn't exist.

But is there a iOS4 blocks equivalent API to:

[button addTarget:self action:@selector(tappy:) forControlEvents:UIControlEventTouchUpInside];

I suppose this could be implemented using a category, but would rather not write this myself due to extreme laziness :)

Something like this would be awesome:

[button handleControlEvent:UIControlEventTouchUpInside withBlock:^ { NSLog(@"I was tapped!"); }];
A: 

I just implemented this. It work's like a charm!

And it wasn't even hard.

Kind regards Martin

typedef void (^ActionBlock)();

@interface UIBlockButton : UIButton {
  ActionBlock _actionBlock;
}

-(void) handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock) action;

@implementation UIBlockButton

-(void) handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock) action{
  _actionBlock = Block_copy(action);
  [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
}

-(void) callActionBlock:(id)sender{
  _actionBlock();
}

-(void) dealloc{
  Block_release(_actionBlock);
  [super dealloc];
}

@end

Martin Reichl
Is there any way to do this to existing buttons via a category? I know the trouble is having an instance variable you can save the block for..
Ben Scheirman