I am writing a subclass of UITableView and want my subclass to process some of the UITableViewDelegate methods before passing them along with the βrealβ delegate, as well as redirect all UITableViewDelegate methods not implemented by my subclass.
In a subclass, I have a private property:
@property (nonatomic, assign) id <UITableViewDelegate> trueDelegate;
which contains the "real delegate" to which all unrealized methods should pass. In both my init methods, I set
self.delegate = self;
and I override - (void) setDelegate: (id) like this
-(void)setDelegate:(id<UITableViewDelegate>)delegate { if (delegate != self) { _trueDelegate = delegate; } else { [super setDelegate:self]; } }
I then redefine them to handle message forwarding
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { NSMethodSignature *sig; sig = [[self.delegate class] instanceMethodSignatureForSelector:aSelector]; if (sig == nil) { sig = [NSMethodSignature signatureWithObjCTypes:"@^v^c"]; } return sig; } - (void)forwardInvocation:(NSInvocation *)anInvocation { SEL selector = anInvocation.selector; if ([self respondsToSelector:selector]) { [anInvocation invokeWithTarget:self]; } else { [anInvocation invokeWithTarget:_trueDelegate]; } }
The problem is that unrealized delegate methods are never called in the table view, so they are not given the opportunity to be redirected to the _trueDelegate object.
I tried checking them out here:
- (BOOL)respondsToSelector:(SEL)aSelector { }
but this method is never called for UITableViewDelegate methods, although it is well versed in other methods.
Lance source share