Created
June 9, 2014 13:55
-
-
Save boundsj/b0d1568ddff863bb9d90 to your computer and use it in GitHub Desktop.
Subclass with Internal Delegate and Message Forwarding
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
#import <UIKit/UIKit.h> | |
@interface MyTextView : UITextView | |
@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
#import "MyTextView.h" | |
// UITextViewDelegate impl for internal use only by MyTextView | |
@interface MyTextViewInternalDelegate : NSObject <UITextViewDelegate> | |
@property (weak, nonatomic) id<UITextViewDelegate> userDelegate; | |
@end | |
@interface MyTextView () | |
@property (strong, nonatomic) MyTextViewInternalDelegate *internalDelegate; | |
@end | |
@implementation MyTextView | |
- (id)initWithCoder:(NSCoder *)coder { | |
self = [super initWithCoder:coder]; | |
if (self) { | |
self.internalDelegate = [[MyTextViewInternalDelegate alloc] init]; | |
[super setDelegate:self.internalDelegate]; | |
} | |
return self; | |
} | |
#pragma mark UITextViewDelegate custom work | |
- (void)didBeginEditing { | |
NSLog(@"================> whatever MyTextView needs to do internall as a delegate"); | |
} | |
#pragma mark Overrides | |
- (void)setDelegate:(id<UITextViewDelegate>)delegate { | |
self.internalDelegate.userDelegate = delegate; | |
} | |
- (id<UITextViewDelegate>)delegate { | |
return self.internalDelegate.userDelegate; | |
} | |
@end | |
@implementation MyTextViewInternalDelegate | |
- (void)textViewDidBeginEditing:(UITextView *)textView { | |
[(MyTextView *)textView didBeginEditing]; | |
if ([self.userDelegate respondsToSelector:_cmd]) { | |
[self.userDelegate textViewDidBeginEditing:textView]; | |
} | |
} | |
#pragma mark Message Forwarding | |
- (BOOL)respondsToSelector:(SEL)selector { | |
return [self.userDelegate respondsToSelector:selector] || [super respondsToSelector:selector]; | |
} | |
- (void)forwardInvocation:(NSInvocation *)invocation { | |
[invocation invokeWithTarget:self.userDelegate]; | |
} | |
@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
#import <UIKit/UIKit.h> | |
@class MyTextView; | |
@interface TestViewController : UIViewController | |
@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
#import "TestViewController.h" | |
#import "MyTextView.h" | |
@interface TestViewController () <UITextViewDelegate> | |
@property (weak, nonatomic) IBOutlet MyTextView *myTextView; | |
@end | |
@implementation TestViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.myTextView.delegate = self; | |
} | |
#pragma mark <UITextViewDelegate> | |
- (void)textViewDidBeginEditing:(UITextView *)textView { | |
NSLog(@"================> user delegate gets to do stuff too"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment