Created
December 13, 2010 09:29
-
-
Save kgn/738828 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; | |
@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 |
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 | |
- (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