Created
January 15, 2013 12:29
-
-
Save pyrtsa/4538272 to your computer and use it in GitHub Desktop.
Blocks instead of delegates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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