Skip to content

Instantly share code, notes, and snippets.

@eiffelqiu
Forked from kgn/Delegate.h
Created May 22, 2011 08:44
Show Gist options
  • Save eiffelqiu/985279 to your computer and use it in GitHub Desktop.
Save eiffelqiu/985279 to your computer and use it in GitHub Desktop.
Objective-c delegate
@protocol MyControlDelegate <NSObject>
@optional
- (void)delegateAction:(id)value;
@end
@interface MyControl : NSObject{
id<MyControlDelegate> delegate;
}
@property (nonatomic, assign) id<MyControlDelegate> delegate;
- (id)initWithDelegate:(id<MyControlDelegate>)aDelegate;
+ (id)objectWithDelegate:(id<MyControlDelegate>)aDelegate;
@end
@implementation MyControl
@synthesize delegate;
- (id)initWithDelegate:(id<MyControlDelegate>)aDelegate{
if((self = [super init])){
self.delegate = aDelegate;
}
return self;
}
+ (id)objectWithDelegate:(id<MyControlDelegate>)aDelegate{
return [[[[self class] alloc] initWithDelegate:aDelegate] autorelease];
}
- (void)action{
if(self.delegate && [self.delegate respondsToSelector:@selector(delegateAction:)]){
[self.delegate delegateAction];
}
}
- (void)dealloc{
delegate = nil;
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment