-
-
Save eiffelqiu/985279 to your computer and use it in GitHub Desktop.
Objective-c delegate
This file contains 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
@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 |
This file contains 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
@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