Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created January 15, 2013 12:29
Show Gist options
  • Select an option

  • Save pyrtsa/4538272 to your computer and use it in GitHub Desktop.

Select an option

Save pyrtsa/4538272 to your computer and use it in GitHub Desktop.
Blocks instead of delegates
// Widget.h
#import <UIKit/UIKit.h>
@interface Widget : UIView
@property (nonatomic, copy) void (^action)(Widget *widget, NSUInteger someArg);
@end
// Widget.m
#import "Widget.h"
// ...
- (void)somethingHappenedWithArgument:(NSUInteger)arg
{
if (self.action) self.action(self, arg);
}
// usage in ViewController.m:
{
__weak ViewController *weak = self;
Widget *widget = ...;
widget.action = ^(Widget *widget, NSUInteger someArg) {
ViewController *self = weak;
[self updateStateWithArg:someArg];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment