Udostępniony UITableViewDelegate
Piszę podklasę UITableView i chcę, aby moja podklasa obsługiwała niektóre metody UITableViewDelegate przed przekazaniem ich do „prawdziwego” delegata, a także przekazała wszystkie metody UITableViewDelegate nie zaimplementowane przez moją podklasę.
W podklasie mam własność prywatną:
@property (nonatomic, assign) id <UITableViewDelegate> trueDelegate;
który zawiera „prawdziwego delegata”, do którego wszystkie niezrealizowane metody powinny zostać przekazane. W obu moich metodach inicjowania ustawiłem
self.delegate = self;
i I override - (void) setDelegate: (id) w ten sposób
-(void)setDelegate:(id<UITableViewDelegate>)delegate {
if (delegate != self) {
_trueDelegate = delegate;
} else {
[super setDelegate:self];
}
}
Następnie zastępuję je, aby obsłużyć przekazywanie wiadomości
-(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];
}
}
Problem polega na tym, że niezaimplementowane metody delegowania nigdy nie są wywoływane w widoku tabeli, dlatego nie mają szansy na przekazanie ich dalej do obiektu _trueDelegate.
Próbowałem je sprawdzić tutaj:
- (BOOL)respondsToSelector:(SEL)aSelector {
}
ale ta metoda nigdy nie jest wywoływana dla metod UITableViewDelegate, chociaż dobrze wychwytuje inne metody.