从子类访问超类的委托

时间:2014-08-20 09:44:22

标签: ios objective-c inheritance delegates

我有四个类 MainVC ParentClient ChildClient1 ChildClient2 (它们是ParentClient的子类)。 ParentClient具有MainVC的委托,以便在MainVC中

- (void)viewDidLoad
{
      [super viewDidLoad];
      [ParentClient instance].mainViewDelegate = self;
}

然后ParentClient看起来像这样

@interface BaseClient : NSObject

@property (assign) id<MainVCInteraction> mainViewDelegate;
+(instancetype) instance;

@end

现在我想从 ChildClient1 ChildClient2 访问mainViewDelegate,当[ParentClient instance].mainViewDelegate返回正确的值时,它返回nil

1 个答案:

答案 0 :(得分:0)

这就是我所做的我移除了BaseClient类,以便ChildClient1,ChildClient2不再是BaseClient的子类。我定义了一个objective-c 协议文件MainVCInteaction.h,并使Client1,Client2看起来像这样:

#import "MainVCInteraction.h"
@interface ChildClient1 : NSObject

@property (assign) id<MainVCInteraction> mainViewDelegate;
+(instancetype) instance;

@end


#import "MainVCInteraction.h"
@interface ChildClient2 : NSObject

@property (assign) id<MainVCInteraction> mainViewDelegate;
+(instancetype) instance;

@end

然后MainVC实现了这个协议,我像这样分配了委托

- (void)viewDidLoad
{
      [super viewDidLoad];
      [ChildClient1 instance].mainViewDelegate = self;
      [ChildClient2 instance].mainViewDelegate = self;
}