Skip to content

Instantly share code, notes, and snippets.

@kgn
Created December 13, 2010 09:29
Show Gist options
  • Save kgn/738828 to your computer and use it in GitHub Desktop.
Save kgn/738828 to your computer and use it in GitHub Desktop.
Objective-c delegate
@protocol MyControlDelegate;
@interface MyControl : NSObject
@property (nonatomic, weak) id<MyControlDelegate> delegate;
- (id)initWithDelegate:(id<MyControlDelegate>)aDelegate;
+ (id)objectWithDelegate:(id<MyControlDelegate>)aDelegate;
@end
@protocol MyControlDelegate <NSObject>
@optional
- (void)delegateAction:(id)value;
@end
@implementation MyControl
- (id)initWithDelegate:(id<MyControlDelegate>)delegate{
if((self = [super init])){
self.delegate = delegate;
}
return self;
}
+ (id)objectWithDelegate:(id<MyControlDelegate>)delegate{
return [[[self class] alloc] initWithDelegate:delegate];
}
- (void)action{
if([self.delegate respondsToSelector:@selector(delegateAction:)]){
[self.delegate delegateAction];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment