다른 프로토콜을 자체적으로 상속하는 프로토콜을 정의하는보기 컨트롤러가 있습니다. 내 프로토콜을 구현하는 모든 개체에서 상속 된 프로토콜을 구현하기를 원합니다.objective C forwardingTargetForSelector가 항상 작동하지 않는 메시지 전달
내가 내부적으로 몇 가지를 구성하기 위해 상속 된 프로토콜의 메시지의 일부를 가로 채기 위해 내 수업을 설정하려는 결국
내가 쓸 수있는 내 클래스의 대리인에게 모든 메시지를 전달하고자 많은 보일러 플레이트 코드가 프로토콜과 인턴을 모두 스텁 (stub)하기 위해 위임자를 호출하지만 많은 시간을 쪼개는 것을 볼 수 있습니다. "수퍼"프로토콜이 변경되면이 클래스를 다시 복원해야합니다.
사용자 정의 UI 컨트롤에서 매우 두드러진 부분입니다. 예를 들어 테이블 또는 컬렉션 뷰와 같은 기존 구성 요소를 재사용 할 때는 데이터 소스가 모든 일반 프로토콜에 응답하기를 원하지만 일부 인스턴스에서는 인덱스에 따라보기를 구성하거나 특정 상태를 저장해야합니다.
내가 응답하지 않는 메시지를 전달하기 위해 forwardingTargetForSelector
를 사용하여 시도했지만, 항상 전달되지 ... 여기
Class A: (the top most protocol)
@
protocol classAProtocol <NSObject>
-(void)method1;
-(void)method2;
-(void)method3;
@end
My Class
@protocol MyClassProtocol <classAProtocol>
-(void)method4;
@end
@interface MyClass
@property (nonatomic,weak> id <MyClassProtocol> delegate;
@end
@interface MyClass (privateInterface)
@property (nonatomic,strong) ClassA *classAObject;
@end
@implementation MyClass
-(init)
{
self = [super init];
if (self)
{
_classAObject = [[ClassA alloc] init];
_classAObject.delegate = self; // want to answer some of the delegate methods but not all
}
}
-(void)method1
{
// do some internal configuration
// call my delegate with
[self.delegate method1];
}
-(id)forwardingTargetForSelector:(SEL)aSelector
{
if ([self respondsToSelector:aSelector])
{
return self;
}
if ([self.delegate respondsToSelector:aSelector])
{
return self.delegate;
}
return nil;
}
-(void)setDelegate:(id <MyClassProtocol>)delegate
{
self.delegate = delegate; // will forward some of the messages
}
@end
이 솔루션을 시도하고 다시 연락 할 것이다. –